WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Exclude children from category page. (5 posts)

  1. SubjectEgo
    Member
    Posted 6 years ago #

    I'm building a category template page, which I would like to exclude child categories from appearing on. The page should display posts from the category selected, and no posts from any of its child categories.

    I know there's an easy way about this. I just don't have the time right now to figure it out myself.

  2. MichaelH
    Member
    Posted 6 years ago #

    Posted part an example loop you might try in your category template file.

    See http://wordpress.pastebin.ca/758551

    Resources:
    Category Templates
    Template Hierarchy
    Stepping into Templates Tags
    Editing Files

  3. SubjectEgo
    Member
    Posted 6 years ago #

    A little tweaking and it blends into my site perfectly. Thank you very much.

  4. mishko
    Member
    Posted 6 years ago #

    I tried the suggested code, but it only worked properly on child categories. Children displayed posts that were in their category only, rather than theirs plus the parent posts.

    However, within parent categories, absolutely nothing displays.

    Here's my (abbreviated for posting) code:

    <?php if (have_posts()) : ?>
          <?php $cat = intval( get_query_var('cat') ); ?>
          <?php while (have_posts()) : the_post(); ?>
          <?php if ( in_category($cat) ) { ?>
    
          <?php the_content('Read the rest'); ?>
          <?php } ?>
    
          <?php endwhile; ?>
    
    	   <?php else : ?>
    
    		<h2>Not Found</h2>
    		<p>Uh oh! Sorry, but you are looking for something that isn't here. Please use the form below to search for it:</p>
    		<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    	   <?php endif; ?>

    Since when I'm in a parent category, I don't see the post or the "Not Found" message, it seems that in_category($cat) is returning false. But why would that be? I have verified with echo $cat that $cat is indeed representative of the current category. Any ideas?

  5. mishko
    Member
    Posted 6 years ago #

    I tried the suggested code, but it only worked properly on child categories. Children displayed posts that were in their category only, rather than theirs plus the parent posts.

    However, within parent categories, absolutely nothing displays.

    The parent categories looked like they were showing nothing because they didn't show the only post in the category. I put a second post in the parent category and with the above code it showed the second post, but still not the first post.

    After doing some searching and testing I've found this code to do what I wanted in the beginning.

    <?php if (have_posts()) : ?>
          <?php $cat = intval( get_query_var('cat') );  
    
    $catpostquery = 'cat=' . $cat . '&posts_per_page=1&orderby=date&order=ASC'; ?>
    
          <?php query_posts($catpostquery); ?>
    
           <?php while (have_posts()) : the_post(); ?>
            <?php  if ( in_category($cat) ) { ?>
    
          <?php the_content('Read the rest'); ?>
          <?php  } else { ?>
    
          <?php endwhile; ?>
    
    	   <?php else : ?>
    
    		<h2>Not Found</h2>
    
    	   <?php endif; ?>

    Basically by adding the query_posts function to only show one post and sort the order (before loop) then I get the one and only post in that parent category to show. This also works for the child categories. I'm not entirely sure why this works correctly, but I'll take it.

    If anyone knows better why the first and only post in a parent category wouldn't show with the code I posted before this post, I'd like to know why. Also if there is a better solution, please share.

Topic Closed

This topic has been closed to new replies.

About this Topic