WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Listing child categories and posts (5 posts)

  1. Calicosun
    Member
    Posted 4 years ago #

    Hi

    In my sidebar I would like to have a list of all the child categories and any posts under those child categories of a specific parent category. Here is an example:

    ARTICLES

      Brand

    • Brand consistency
    • Brand something else

      Printing

    • Printing colour
    • Paper

    The parent category is: Site Articles

    I have found two pieces of code that do part of what I want but I am not sure how to integrate the two.

    This code displays all categories.

    <?php
    			//for each category, show posts
    			$cat_args=array(
     			'orderby' => 'name',
      			'order' => 'ASC'
       			);
    			$categories=get_categories($cat_args);
      			foreach($categories as $category) {
        		$args=array(
          		'orderby' => 'title',
          		'order' => 'ASC',
          		'showposts' => -1,
          		'category__in' => array($category->term_id),
          		'category__not_in' => array(4,5,6, 10),
    
          		'caller_get_posts'=>1
       			 );
        		$posts=get_posts($args);
         		if ($posts) {
            	echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name. '</a> ';
            	foreach($posts as $post) {
              	setup_postdata($post); ?>
    
              	 <ul><li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li></ul></li>
              	<?php
            	} // foreach($posts
          		} // if ($posts
        		} // foreach($categories
    			?>

    This code displays the posts from the parent category:

    <?php
    //get all posts for children of category $cata
    $cata = 16;
    $taxonomy = 'category';
    $cata_children = get_term_children( $cata, $taxonomy );
    
    $args=array(
      'category__in' => $cata_children,
      '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 A children';
      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().
    ?>

    I would really appreciate if if someone could help me out by showing me what code I need to do this?

    Thank you so much.

  2. MichaelH
    Member
    Posted 4 years ago #

    Try this:

    <?php
    //get all children of category "Site Articles", then display posts in each cat
    $taxonomy = 'category';
    $param_type = 'category__in';
    $cat_id = get_cat_ID('Site Articles');
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'child_of' => $cat_id
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          '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 in '.$taxonomy .' '.$term->name;
          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. Calicosun
    Member
    Posted 4 years ago #

    Thank you so much for helping me out. I took your code which worked perfectly and made a few minor adjustments so the child category heading linked through to a page that displayed excerpts of the posts in that category and to remove the words 'List of posts in category'. Here is my final code in case it is helpful to anyone else:

    <?php
    //get terms (e.g. categories or post tags), then display all posts in each retrieved term
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'child_of' => 16
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
    
          echo '<li><a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name. '</a> ';
    
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <ul><li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li></ul>
           <?php
                 endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Thanks so much.

  4. AkaMaDdiSk
    Member
    Posted 4 years ago #

    This code seems to do what I need, but how do I exclude specific categories? Thanks in advance for the help.

    <?php
    //get all posts for children of category $cata
    $cata = 16;
    $taxonomy = 'category';
    $cata_children = get_term_children( $cata, $taxonomy );
    
    $args=array(
      'category__in' => $cata_children,
      '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 A children';
      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().
    ?>
  5. mikeboy3
    Member
    Posted 4 years ago #

    Calicosun, your code worked perfectly, however, it is limited to a single category, is there a way to change the 'child_of' => 16 value to automatically detect what category archive I am opening?

Topic Closed

This topic has been closed to new replies.

About this Topic