1

We would like to know that how we occur error when we implement this code in our sidebar.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>';  
}

The error we are getting:

Fatal error: Cannot use object of type WP_Query as array in C:\xampp\htdocs\wordpress\wp-includes\query.php on line 2374

2 Answers 2

8

a possible conflict with the name $posts which is used by wp core; try, for instance:

$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>';   
} 
1
  • Great! Not evidently, but $posts is reserved by WP Core.
    – BasTaller
    Commented Dec 6, 2016 at 23:02
0

hi rodger,

 $query = new WP_Query( array( 'meta_key' => 'Old ID', 'meta_value' => $atts['oldid'] ) );
 return $query['post_title'];

Codex Ref. you should try this..

1
  • without while loop. replace the total code with this new one . or we have to implement by own. and please also suggest me, how to implement our H1 tags, li tags with this new code .
    – PPS
    Commented Jun 30, 2011 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.