2

Trying to solve this issue for the past hour and I can't figure out what's going on.

Trying to create single-blog.php (I have a few posts which are under the category 'blog') but Wordpress automatically redirects these posts to single.php! I've flushed the permalinks by settings > permalink > save changes but still nothing happens. To test & to see if Wordpress picks up on single-blog.php the code I used was:

<?php get_header(); ?>
     <h1>TEST HERE</h1>
<?php get_footer(); ?>
2
  • I had the same problem. I solved it by modifying the Permalink to Post name - /%postname%/ Oct 6, 2015 at 7:50
  • can you not use single-post-blog.php? Mar 15, 2019 at 13:33

1 Answer 1

4

Read Template Hierarchy article in the codex, especially pay attention to Single Post Display part.

As you can see you have only three options:

  1. single-{post_type}.php
  2. single.php
  3. index.php

It means that you can't create a template for posts related to blog category. So you shouldn't use single-blog.php template, use single.php instead and add there something like this:

<?php get_header(); ?>
    <?php if ( has_category( 'blog' ) : ?>
        <h1>TEST HERE</h1>
    <?php else : ?>
        <h1>Else posts</h1>
    <?php endif; ?>
<?php get_footer(); ?>
3
  • 1
    Also, if you need to customize the single-post display for posts in a particular category, if those changes can be pared down to CSS, then you can take advantage of the CSS classes output by body_class() and post_class() to accomplish what you're after. Oct 1, 2013 at 14:06
  • Thanks for the tip. Out of curiosity, when does single-{post_type}.php become relevant if a simple if/else function works? Oct 2, 2013 at 6:39
  • @NazarAbubaker if you use custom post type like product, then you can use single-product.php template for this CPT. Oct 2, 2013 at 6:43

Your Answer

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

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