我们想知道,当我们在侧栏中实现这段代码时,我们是如何发生错误的。php
$categories = get_categories();
foreach($categories as $category)
{
printf(\'<h2>%s</h2><ul>\', $category->cat_name);
$posts = new WP_Query(\'cat=\'.$category->cat_ID);
while($posts->have_posts())
{
$posts->the_post();
echo \'<li>\', the_title(), \'</li>\';
}
print \'</ul>\';
}
我们得到的错误:
致命错误:无法在C:\\xampp\\htdocs\\wordpress\\WP includes\\Query中使用WP\\u Query类型的对象作为数组。php在线2374
SO网友:Michael
可能与名称冲突$posts
由wp core使用;尝试,例如:
$categories = get_categories();
foreach($categories as $category)
{
printf(\'<h2>%s</h2><ul>\', $category->cat_name);
$cat_posts = new WP_Query(\'cat=\'.$category->cat_ID);
while($cat_posts->have_posts())
{
$cat_posts->the_post();
echo \'<li>\', the_title(), \'</li>\';
}
print \'</ul>\';
}