Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Natasha McDiarmid
Natasha McDiarmid
11,400 Points

Having custom single templates based on post category

I have WordPress posts categorized by the 'Meet Our People', it's ID is 7.

How can I create custom single pages or write conditionals to display different content based on what the category ID is?

I did some research and found this as a suggestion:

<?php
  $meet_our_people = $wp_query->post;

  if ( in_category('7') ) {
  include(TEMPLATEPATH . '/single-meet-our-people.php');

  } else {
  include(TEMPLATEPATH . '/single-post.php');

  }
?>

I think it's working, but how can I target other posts that don't use the variable:

 $meet_our_people

This is the loop I have targeted, but for other basic posts, how can I target those?

<?php $meet_our_people= new WP_Query( 'category_name=meet-our-people&posts_per_page=3' );
if ( $meet_our_people->have_posts() ) : ?>

<?php while ( $meet_our_people->have_posts() ) : $meet_our_people->the_post(); ?>

<?php the_post_thumbnail('meet-our-people'); ?>
<?php the_title(); ?>

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else:  ?>
 <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

2 Answers

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Try putting the condition statement inside the loop the using the else statement for the other posts.