Skip Navigation

[Closed] Displaying Child Categories, and their posts, in Parent Category Archives View

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 1 month, 2 weeks ago.

Assigned support staff: Christian Cox.

Author
Posts
#1728227

I have a page that displays all the Parent Categories and their Child Categories using 2 Views:

hidden link

These are normal posts and their categories, not new CPTs. or taxonomies. I have 2 things I'm stuck with:

1) I want to create an Archives page for each Parent category that Lists it's Child Categories and under each Child Category the posts in that Child Category. So on the page above the first box - G-Suite is the Parent Category and clicking that should go to an Archives page with the 3 Child Category headings(Gmail, G-Drive, Calendar) and the posts for each Child Category under each heading.

2) I also need archives page for each Child Category that displays the posts within that Child Category.

Thanks,

Tim

#1728705

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Hello, WordPress typically generates taxonomy archives automatically at the standard URL structure:
https://yoursite.com/category/{term-slug}/
That URL structure also reflects hierarchy, so child term archives utilize a hierarchical structure like:
https://yoursite.com/category/{parent-term-slug}/{child-term-slug}/

Toolset gives you the ability to design the loops of these archives using Toolset's WordPress Archives feature. So for point #2 in your requirements, you would create a WordPress Archive for the category taxonomy, and design the loop however you want it to look to display each post. However for point #1, it sounds like you want a slightly different design for top-level parent term archives, with posts grouped by child term. This isn't supported out-of-the-box with Toolset because:
- Only one WordPress Archive can be applied to the entire taxonomy. You cannot apply different WP Archives to different terms, per hierarchical level.
- Toolset's WordPress Archives show lists of all matching posts, not grouped by child term.

So to get around those two limitations, you need:
A. A way to programmatically apply different WordPress Archive designs to different terms, per hierarchical level. Our PHP APIs give you the ability to do this. I can provide some example code you can use to help work around this problem.
B. A way to display a list of posts grouped by child term, inside a WordPress Archive. You can do this with nested Views as described below. Note that pagination is not well supported in nested Views, so this approach isn't ideal for large sites where you need paginated archives to show many posts.

For A, first you must create two WordPress Archives - one will be applied to parent terms, and the other to child terms. For now, assign to the Category taxonomy the WordPress Archive you create for child terms. Leave the parent term archive unassigned. Note the numeric IDs of each archive - you can find those in the list at Toolset > WordPress Archives.

Here is a custom code snippet you can customize for your site. Place the modified code in your child theme's functions.php file, or create a new code snippet in Toolset > Settings > Custom Code and paste it at the end of the snippet:

// From Toolset Support
// For https://toolset.com/forums/topic/displaying-child-categories-and-their-posts-in-parent-category-archives-view/
function get_tax_level($id, $tax){
    $ancestors = get_ancestors($id, $tax);
    return count($ancestors)+1;
}

add_filter( 'wpv_filter_force_wordpress_archive', 'switch_tax_archive_by_level', 30, 2 );
function switch_tax_archive_by_level( $wpa_assigned, $wpa_loop ) {
  $wpa_to_apply = $wpa_assigned;
  $current_taxonomy = isset(get_queried_object()->taxonomy) ? get_queried_object()->taxonomy : null;
  // only apply to standard category taxonomy archives
  if( !$current_taxonomy || $current_taxonomy != 'category' )
    return $wpa_to_apply;

  $current_term_level = get_tax_level(get_queried_object()->term_id, $current_taxonomy);
  if ($current_term_level == 1) {
    // show top-level archive
    $wpa_to_apply = 123;
  } else {
    // show other-level archive
    $wpa_to_apply = 234;
  }
  return $wpa_to_apply;
}

In this code, change 123 to match the numeric ID of the WordPress Archive you want to display for the top-level parent terms. Change 234 to match the numeric ID of the WordPress Archive you want to display for the other terms. That's it, there should be no other changes required in this code.

Next, update your child term WordPress Archive. In the loop, insert the post title with link for now so we can test and confirm it's working as expected. Then later you can modify this archive to display the posts how you want. Save the archive and visit the corresponding URL of one of your child term slugs, like https://yoursite.com/category/parent-term-slug/child-term-slug. You should see the list of post links. Once that is functional, we can move on to the other archive you created for top-level parent terms.

For B, modify the top-level parent term WordPress Archive so that nothing is included inside the loop tags. If you have not yet created anything in the loop editor, click "Skip Wizard" here to generate the basic loop structure without content. Leave the loop tags empty for now. After that, separately create a View of Category taxonomy terms. Note that it is not possible to create this View in the Block Editor. Only post Views can be built in the Block Editor. In the View editor, use the Query Filter panel to add a taxonomy filter based on the parent term. Configure it so that the parent term is set by the current taxonomy archive. If you cannot find the Query Filter panel, scroll to the top right corner of the screen and click "Screen Options". You can enable the panel here. Use the Loop Wizard to insert in the loop just the term link for now, so we can test and confirm it is working correctly. Save the View of terms, and return to edit the parent term WordPress Archive. Insert this View of terms in archive just before the wpv-items-found shortcode. Save the archive and visit the corresponding URL of one of your parent terms, like https://yoursite.com/category/parent-term-slug/

You should see a list of child terms, each linked to its term archive page. Click those links, and you should be redirected to the child term archive you designed earlier. If the links from parent term to child term archives are working, and the two archive designs are applied correctly to each term archive page, move on to the next step.

Create a View of posts and add a Taxonomy Term Query Filter, where the term is set by the parent View of terms. Use the Loop Wizard to insert the post title with a link for now, so we can test and confirm this is working correctly. Once you have added the post link to the loop, save the View and return to the View of terms. Now in the loop, just after the term link shortcode, insert the View of posts you just created. Save the View and return to the parent term archive in your browser. You should now see a list of posts appear below each term.

Once you get through this step, you have everything set up and functional. You can go back and modify the design of the loop items as needed to get exactly what you want. If you get stuck setting things up, feel free to reach out and let me know where you are having trouble. I can offer more guidance.

#1730353

Hi Chirstian,

Terrific instructions. I think I have it working as I want it. But I will do some more testing an let you know aftr the weekend.

Tim

#1731455

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Sounds good - I'll stand by for your update.

The topic ‘[Closed] Displaying Child Categories, and their posts, in Parent Category Archives View’ is closed to new replies.