Only show posts from current category (3 posts)

  1. MatthewN
    Posted 3 years ago #

    Hi all,
    I have a question regarding the category page. Basically I have split my posts into categories and some categories have sub categories but the posts are always only in the last category level. So for example Photos > Travel > Africa - The post is in the final category level "Africa". There a no posts in the intermediate category "Travel".

    My question is, how can I get WordPress to only show the posts in the current category you are on? By default it seems that if you are on the "Photos" category it will display all the posts in the sub categories. I'd like to only display a menu of the categories and then show the posts once you get down to the category that actually contains the posts. Hope that makes sense!

    I am hoping to achieve this by editing the category.php file and to not use anything static. As the category structure may change etc. I appreciate any help, Thanks.

  2. Michael
    Part-Time Forum Moderator
    Posted 3 years ago #

    to show only post of the current category, try to add some code like this before the start of the loop in category.php:

    <?php global $wp_query;
    $args = array_merge( $wp_query->query, array( 'category__in' => array(get_query_var('cat')) ) );
    query_posts( $args );
  3. MatthewN
    Posted 3 years ago #

    Thanks for your suggestion, I found another way that worked, to edit the code just after the start of the loop:

    if ((is_category()) && in_category($wp_query->get_queried_object_id())) {
    get_template_part( 'content', get_post_format() );
    } ?>

    Although, I decided to go with your suggestion as it seems like a better way to accomplish this. Thanks a lot for your help, if you know any resources or anything that would help me develop the category page further I'd appreciate it it. I am trying to make it so users can browse the categories in steps so to speak. Thanks again.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.