Wordpress, can't get get_posts method to work correctly

Discussion in 'WordPress' started by GSto, Sep 24, 2008.

  1. #1
    I am trying to write an add-on to my wordpress blog that show the most recent post in each category. whenever I try to use the get_posts() method, it always returns the most recent post, regardless of what argument I use. even if I tell it to get 5 posts, it just returns the first post 5 times! here is a snippet of the code I am using:

    function the_category_box(){
    
         $catIDs = get_all_category_ids(); 
         $control = 0;  //total number posted so far
         $columns = 3; //number of columns in table
         
         echo "<table>";
         foreach($catIDs as $cat){
    	         $catName = get_catname($cat); //getting cagegory name
    	         $args = array(
    	                 numberposts => 1
                             cat => $cat
    	                 );
    	         $catPosts = get_posts($args); //<-- THE PROBLEM
    	         if($control%$columns == 0){   echo "<tr>";   } 
    	         echo "<td> <h4> Latest in <b>$catName</b></h4>";
    	             foreach($catPosts as $post){
    		             setup_postdata($post);
    			         the_title();
    			         echo "<a href=\"";
    			         the_permalink();
    			         echo"\">Read More--></a>";
    	             }
    	         echo"</td>";
    	         if($control%$columns == 2){   echo "</tr>";   }
    	         $control++;
              }     
         }
         echo "</table></div>";   
         return;
    }
    
    PHP:
    Is there a better way to do this?
     
    GSto, Sep 24, 2008 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    That argument list doesn't look right. Going by the codex, there should be single quotes, a separating comma, and cat should be category.
    
    $args = array(
         'numberposts' => 1,
         'category' => $cat
         );
    
    Code (markup):
     
    Cash Nebula, Sep 24, 2008 IP
  3. GSto

    GSto Peon

    Messages:
    218
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    tried that, same problem.
     
    GSto, Sep 25, 2008 IP
  4. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Maybe it's because the_title() and the_permanlink() are only used in the Wordpress loop. I think you need to read them differently in plugins.

    I haven't tried this but maybe it will work:
    
         $catIDs = get_all_category_ids(); 
         $control = 0;  //total number posted so far
         $columns = 3; //number of columns in table
         global $post;
    
         .........
    
         foreach($catPosts as $post){
              setup_postdata($post);
              echo $post->post_title;
              echo "<a href=\"";
              echo get_permalink($post->ID);
              echo"\">Read More--></a>";
         }
    
    Code (markup):
     
    Cash Nebula, Sep 25, 2008 IP
  5. GSto

    GSto Peon

    Messages:
    218
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, I'll give that a shot later. sounds like that could be the fix to the problem.
     
    GSto, Oct 14, 2008 IP