How to List Posts by Sub category
-
I thought this code snippet might be useful for someone. It lists posts by sub categories. It might be useful (for example) if you wanted to have jobs listed in various sub-categories of the ‘jobs’ category all with their own sub heading (so, for instance, ‘jobs in asia’)- and have them all display in one page template (say, on your careers page). Here’s how you would do it.
<div id="job-listings"> <?php $cat = 'Jobs'; $catID = get_cat_ID($cat); $subcats = get_categories('child_of=' . $catID); foreach($subcats as $subcat) { echo '<h3>Jobs Opening in ' . $subcat->cat_name . '</h3>'; echo '<ul>'; $subcat_posts = get_posts('cat=' . $subcat->cat_ID); foreach($subcat_posts as $subcat_post) { $postID = $subcat_post->ID; echo '<li>'; echo '<a href="' . get_permalink($postID) . '">'; echo get_the_title($postID); echo '</a></li>'; } echo '</ul>'; } ?> </div>
Poached and refactored from WP-Questions: https://www.wpquestions.com/question/show/id/86
- The topic ‘How to List Posts by Sub category’ is closed to new replies.