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?
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):
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):