Support » Themes and Templates » Only show posts from current category

  • 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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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 );

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Only show posts from current category’ is closed to new replies.