WordPress.org

Ready to get started?Download WordPress

Forums

List sub categories, and associated posts (4 posts)

  1. ikjosh
    Member
    Posted 3 years ago #

    Hi,

    I created a custom category page template. On this template, I want to list the 3 sub categories. Then, under each sub category, I want to list the posts that are filed under them. So, it would look like:

    Parent
    -Sub Category 1
    --Post 1
    --Post 2

    -Sub Category 2
    --Post 1
    --Post 2

    -Sub Category 3
    --Post 1
    --Post 2

    How can I do this?

    Thanks

  2. MichaelH
    Member
    Posted 3 years ago #

    See if this works for you:

    <?php
    //get all child categories for category 15, then for each child category display the posts
    $parent_cat = 15;
    $taxonomy = 'category';
    $cat_children = get_term_children( $parent_cat, $taxonomy );
    
    if ($cat_children) {
      echo '<p>Category 15</p>';
    	foreach($cat_children as $category) {
        $args=array(
          'cat' => $category,
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts belonging to Category ' . $category;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
          endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. 314media
    Member
    Posted 3 years ago #

    *awesome

  4. jonnyplow
    Member
    Posted 3 years ago #

    How can I do this for custom post type taxonomies?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags