I have two Custom Post Types Video and Gallery and a Custom Taxonomy Status with the term Featured.

Right now I'm showing these CPT in my index.php, but I want to style each of them, so I'm using get_template_part to achieve it.

The problem comes when I want to get templates for an specific term combined with CPT, in this case Featured and Video. It works with regular post with the term Featured, but no with the Custom Post Type.

This is my code:

<?php if (have_posts()) : ?>

 <?php while (have_posts()) : the_post(); ?>


            if ( get_post_type() == 'videos' ) : ?>

                <?php get_template_part( 'frontales/front', 'video' ); ?>

            <?php elseif ( has_term( 'featured', 'status', $post->ID)):?>

               <?php get_template_part( 'frontales/front','video-featured' ); ?>

            <?php elseif ( get_post_type() == 'gallery' ) : ?>

                <?php get_template_part( 'frontales/front', 'gallery' ); ?>

            <?php else: ?>     

            <?php get_template_part( 'frontales/front', 'news' ); ?> 

            <?php endif; ?>

        <?php endwhile; ?>

        <?php endif; ?>

1 Answer 1


Your order of your if/else statement is wrong. You would want to have your complex condition (or most important condition) at the top and the simplest (or least important condition) at the bottom.

if/else statements work on the basis that the first condition that hits true will be executed. In your example above, if you have a post that belongs to the specified term and post type, and you first check for either the post type or term, that condition will always ring true and fire regardless of any other condition after that, and the condition which checks whether or not your post belongs to the specified term and post type will never fire although it also rings true.

You would need to reconstruct your conditional statement to look something like

if (    'videos' === get_post_type 
     && has_term( 'featured', 'status' ) 
) {
    // load template when post has the desired term and post type
} elseif ( 'videos' === get_post_type() ) {
    // Load template for videos post post type
} elseif ( has_term( 'featured', 'status' ) ) {
    // Load template for posts attached to the featured term
} elseif ( 'gallery' === get_post_type() ) {
    // Load template for posts from the gallery post type
} else {
    // Load a default template for all other posts
  • That works perfectly, thanks for your time, it's a very good lesson, I'm beginning with PHP :) Commented Jan 11, 2016 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.