Only show post from specific category (by name), but not from subcategory
-
Hello there,
I’ve got an issue with getting posts from a specific category, without getting posts from its subcategory.
—
I’ve got a sidebar that only shows a post regarding to an event. In fact, it only shows the last post of a category (named “competition”), as the other posts from that category are automatically closed at a defined time.
By “closed” I mean that they are moved into another category which is a child of the main category “competition”. Subcategory is name “closed”, in this example.So, I would like to only show the last article from a category, but not the articles from the subcategory.
I would also like to work with categor_name, not ID.
Here is the code which is currently working for the parent category, but still shows posts from the children (or sub-) category.
<?php query_posts('category_name=competition,-closed'); if (have_posts()) : $count = 0; while (have_posts()) : the_post(); $count++; if ($count <= 1) : ?><div id="giveAway" class="sidebar shadow"> <a href="<?php the_permalink(); ?>"> <div class="title"> <h2>Competition<img src="<?php bloginfo("stylesheet_directory"); ?>/design/iconLink.png" /></h2> </div> <div class="content"> <?php MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image'); ?> </div> </a> </div><?php endif; endwhile; endif; ?>
I thought that the “query_posts(‘category_name=competition,-closed’)” code would exclude the subcategory from the query, but it seems not to work.
Is there any work around?
Séane
- The topic ‘Only show post from specific category (by name), but not from subcategory’ is closed to new replies.