How can I make a post that belongs to a category or have specific tags, display different from the other single posts?

How can I make a single post display different from the other single posts when I add it to a specific category.

example: I have a post that is in 3 categories: music, songs & video clips. I want every time that I add a post in category “songs”, it will be displayed differently from all of the other default single posts that don’t belong to the category songs. How can I do that?
Also, can the same thing happen with tags too? Meaning, when I add to a post some specific tags can it change the template of the post?

Related posts

Leave a Reply

1 comment

  1. With CSS: If your theme uses post_class() on a containing element, you can target that element with the class .category-songs to control styling.

    With a template filter: add a filter to single_template and check the assigned categories for your songs category, and use the template songs-single.php if that category is found:

    function wpse_check_single_categories( $template = '' ){
        $categories = get_the_category();
        foreach( $categories as $cat ):
            if( $cat->name == 'songs' ):
                $template = locate_template( array( "songs-single.php", $template ), false );
            endif;
        endforeach;
        return $template;
    }
    add_filter( 'single_template', 'wpse_check_single_categories' );