Hey all. I've written a totally useful and effective function that will sort through each of the sub-categories of a specified wordpress category, and list the most recent 10 articles within each sub-category. All is working well, but I fear my code may not be as efficient as it could be. I currently have a master category called "Articles" with four sub-categories, but this function scales as I add more sub-categories. The below function is adding 28 queries to my page...bringing me to a grand total of 69 queries.
Any input would be terrific. Thanks!
<?php
//FUNCTION LOADS ALL SUBCATEGORIES FROM A SPECIFIED CATEGORY AND THEN
//LISTS ARTICLES WITHING THAT CATEGORY IN AN ORDERED LIST
$cat = 1; //manually specify category #1 which happens to be ARTICLES
$displaynum = 10; //specify the # of articles headlines to show in each sub-category
$args=array(
'orderby' => 'name',
'order' => 'ASC',
'title_li' => '',
'hide_empty' => '0',
'parent' => $cat
);
$categories=get_categories($args);
//LOOP THROUGH EACH SUBCATEGORY OF #1 ABOVE AND FORMAT IN DIV
$countbox = 1;
foreach($categories as $category) {
echo '<div id="'.$category->slug.'" class="catbox ';
//add right-padding to odd numbered boxes which appear on the left
if( $odd != $countbox%2 ) echo 'leftbox'; else echo 'rightbox';
echo '">';
echo '<h3>'.$category->name.'</h3>';
//OUTPUT HEADLINES FROM EACH SUBCATEGORY IN A LIST
query_posts('category_name='.$category->slug.'&showposts='.$displaynum); {
echo "\n<ul>";
while (have_posts()) : the_post();
echo '<li>';
echo '<a href="';
the_permalink();
echo '">';
the_title();
echo '</a></li>';
endwhile;
echo "</ul>\n";
}
echo '
<a class="more" href="' . get_category_link( $category->term_id ) . '"
title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>read more</a> ';
echo '</div><!--end '.$category->slug.' div-->';
echo "\n";
$countbox++;
}
?>