Detect which category is being viewed?
-
I built a twentyten child theme and I an having some trouble with displaying featured content on Category pages. Here is a basic example of my code, on category.php
<div class="featured"> <?php if (in_category('news',$wp_query->post->ID)) : ?> <?php query_posts('category_name=news&showposts=1'); ?> <?php while (have_posts()) : the_post(); ?> <?php the_excerpt(); ?> <?php endwhile;?> <?php elseif (in_category('reviews',$wp_query->post->ID)) : ?> <?php query_posts('category_name=reviews&showposts=1'); ?> <?php while (have_posts()) : the_post(); ?> <?php the_excerpt(); ?> <?php endwhile;?> <?php endif; ?> <?php wp_reset_query(); ?> </div><!-- End Featured -->
This
<div class="featured">
sits at the top of the page before the loop of all posts in the category. It doesn’t actually call any featured posts, it takes the most recent post from whatever category I’m viewing and displays it as a featured post. So basically it takes regular posts and displays them in a featured area. If I’m viewing the “news” category then a “news” post is featured and if I’m viewing the “reviews” category than a “reviews” post is featured.So far this works great and I don not have a problem with it. The problem I foresee is having to add another “elseif” statement for every new category. The site will eventually have hundreds of categories and I dont want to have to update the template every-time a category is added.
So, My question is:
Is there a way to do this with one “ifelse” statement?
Is there a way to detect which category is being viewed, and then run the query_posts against that?I hope that explains my predicament, sorry for such a long post.
Thanks!
- The topic ‘Detect which category is being viewed?’ is closed to new replies.