1

I'm trying to solve this for a while, but I couldn't get a satisfactory way to get what I want. Maybe it's very simple and I'm just too inexperienced.

I need a way to display all the article titles of the child categories of a given category, like this:

Category "x"
  Subcategory 1xa
    Post 1xa
    Post 2xa
  Subcategory 2xb
    Post 1xb
    Post 2xb
Category "y"
  Subcategory 1ya
    Post 1ya
    Post 2ya
  Subcategory 2ab
    Post 1yb
    Post 2yb
etc

I really can't do this using my limited knowledge. All I could get was child category listing (without the post titles) or a listing of every category, subcategory and posts. I want to get every categories, child categories and the posts come under them( except uncategorized ones), as in the above order. I saw many posts, but didnt got a solution since I'm not well in coding.

Could anyone help me with this? Thank you very much in advance!

<?php

$cat_args = array(

    'show_option_all'  => '',
    'orderby'          => 'name',
    'order'            => 'ASC'
 );

 $categories = get_categories( $cat_args );

 foreach( $categories as $category ) {

     $args = array(

         'showposts'       => -1,
         'category__in'    => array( $category->term_id ),
         'caller_get_posts'=> 1
     );

     $posts = get_posts( $args );

     if( $posts ) {

         echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';

         foreach($posts as $post) {

             setup_postdata( $post ); ?>

             <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php

         } // foreach($posts
     } // if ($posts
} // foreach($categories
?>
3
  • Show us what you've got so far a.k.a include your code. :)
    – N00b
    Mar 4, 2016 at 9:37
  • @TwerkingN00b I have added the code. please help.
    – codieboie
    Mar 4, 2016 at 10:09
  • caller_get_posts was depreciated in ancient times. Just after the dinosaurs got extinct, it was changed to ignore_sticky_posts. BTW, your code is extremely expensive to run Mar 4, 2016 at 11:44

1 Answer 1

0

I would first put all parent categories into one array – then run through that array and get all child categories and their respective posts.

So you could basically build a loop 'around' your loop – first loop only queries to a depth of 1 (parent cats) and then the second loop looka a lot like yours, but has the "cild_of" argument. So in this second step you get the child cats and their posts for each parent cat.

// First get all parent cats

$parent_cat_args = array(
    'depth'   => 1,
    'orderby' => 'name',
    'order'   => 'ASC'
);

$parent_cats = get_categories($parent_cat_args);

and then for each parent…

// then get all child cats and their posts

$child_cat_args = array(
    'child_of' => $parent_cat_id,
    'orderby'  => 'name',
    'order'    => 'ASC'
);

$child_cats = get_categories($child_cat_args);

foreach($child_cats as $category) {
    $args = array(
        'showposts' => -1,
        'category__in' => array($category->term_id),
        'ignore_sticky_posts'=>1
    );

    $posts=get_posts($args);

    if ($posts) {

    // …

    }
}

You'll find more details about this over at https://developer.wordpress.org/reference/functions/get_categories/

3
  • caller_get_posts was depreciated in ancient times. Just after the dinosaurs got extinct, it was changed to ignore_sticky_posts. BTW, your code is extremely expensive to run Mar 4, 2016 at 11:44
  • thanks @PieterGoosen – for my excuse: I basically just copied the OP's code… BTW: what 'cheaper' alternative would you suggest? ;) Mar 4, 2016 at 12:56
  • You can look into something like this, which is in actual fact a good duplicate to the question ;-) Mar 4, 2016 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.