Support » Fixing WordPress » Using a different template for posts within a specific category

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Do you want it for a single post template (single.php) or for a category archive page?

    You can use conditional tags (has_term) and the get_template_part function in your theme’s single.php to include different template parts. Here is an example:

    <?php
    // 'cat-1' and 'cat-2' are category slugs
    
    if(has_term('cat-1', 'category', $post)) {
      // use template file single-template-cat-1.php
      get_template_part('single-template', 'cat-1');
    }
    elseif(has_term('cat-2', 'category', $post)) {
      // use template file single-template-cat-2.php
      get_template_part('single-template', 'cat-2');
    } else {
      // use default template file single-template.php
      get_template_part('single-template');
    }
    ?>

    For this example you need to create tree new template files: ‘single-template.php’, ‘single-template-cat-1.php’ and ‘single-template-cat-2.php’.

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Or do it from your theme’s functions.php by selecting what template file to use for single posts based on the category that is assigned to the post. Another example:

    add_action('template_include', 'load_single_template');
      function load_single_template($template) {
        $new_template = '';
    
        // single post template
        if( is_single() ) {
          global $post;
          // 'cat-1' and 'cat-2' are category slugs
    
          if( has_term('cat-1', 'category', $post) ) {
            // use template file single-template-cat-1.php
            $new_template = locate_template(array('single-template-cat-1.php' ));
          }
    
          if( has_term('cat-2', 'category', $post) ) {
            // use template file single-template-cat-2.php
            $new_template = locate_template(array('single-template-cat-2.php' ));
          }
    
        }
        return ('' != $new_template) ? $new_template : $template;
      }

    For this example you need to create two new single template files: ‘single-template-cat-1.php’ and ‘single-template-cat-2.php’.

    Great answer, this is exactly what I was looking for. 🙂 Thanks!

    The functions.php version came just after my comment. In this case I would not have to modify my single.php page. I think this one works even better. Or easier.

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    In this case I would not have to modify my single.php page.

    That’s correct. I’ve made some edits to the functions.php code so if you use it use it and it’s different from the one you are using, update the (better) code.

    This is exactly what I was looking for. Thanks for sharing.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Using a different template for posts within a specific category’ is closed to new replies.