WordPress.org

Ready to get started?Download WordPress

Forums

Using a different template for posts within a specific category (7 posts)

  1. Jack
    Member
    Posted 1 year ago #

    How can I do this or is there a plugin for this? The only plugin I found so far is this one, but it got too many 'not working' notes: http://wordpress.org/extend/plugins/custom-single-post-templates-manager/

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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'.

  3. keesiemeijer
    moderator
    Posted 1 year ago #

    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'.

  4. Jack
    Member
    Posted 1 year ago #

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

  5. Jack
    Member
    Posted 1 year ago #

    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.

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    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.

  7. Revansiddha
    Member
    Posted 7 months ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic