I wish to create a 'grid' showing all my sub-categories of a category and the top three posts for each.
Basically I just need this architecture:
- sub-category name
- post 1
- post 2
- post 3
- sub-category name
- post 1
- post 2
- post 3
- sub-category name
- post 1
- post 2
- post 3
etc.
I'm trying to make a new function from wp_list_cats that can include get_posts, or is there a better way?
Sorry I forgot to add what I currently have - It works okay but it seems messy:
function list_posts() {
$ch = get_query_var('cat');
$cat_ids = get_all_category_ids("child_of=$ch");
foreach ( $cat_ids as $cat_id ) {
?>
<h4><?php echo get_cat_name($cat_id); ?></h4>
<ul>
<?php
global $post;
$myposts = get_posts("numberposts=3&offset=1&category=$cat_id");
foreach($myposts as $post) :
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); echo "- $cat_id"; ?></a></li>
<?php endforeach; ?>
<li>More...</li>
</ul>
<?php
}
}
?>
Since I can't edit the post above now (is only one edit allowed?) I have actually got code I am happy with - I'd be interested to know your thoughts on it:
<?php
$parent = get_query_var('cat');
$categories = get_categories("orderby=id&show_count=1&hide_empty=0&child_of=$parent");
foreach ($categories as $cat) {
echo '<h4><a href="'.$cat->category_nicename.'">'.$cat->cat_name."</a></h4>\n";
$cid = $cat->cat_ID;
echo "<ul>\n";
global $post;
$myposts = get_posts("numberposts=3&offset=0&category=$cid");
foreach($myposts as $post) :
echo '<li><a href="'; the_permalink(); echo '">'; the_title(); echo "</a></li>\n";
endforeach;
echo '
<li><a href="' . get_category_link($cid) . "\">More...</a></li>
</ul>\n\n";
}
?>
Hinch, I'm looking to do this exact same thing on one of my websites. How'd this work out for you? Did you have to create a new function in "theme functions" to do this?