Forums

How to List Posts by Sub category (1 post)

  1. Devin Price
    Member
    Posted 2 years ago #

    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: http://www.wpquestions.com/question/show/id/86

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 2 years ago by Devin Price
  • This topic is not a support question
  • WordPress version: 2.9.2