• Resolved mmacneil

    (@mmacneil)


    Hi guys, realised this might be better to post here!

    I have a default post category called Relief Fund which has the slug relief-fund.

    I understand from the docs that I should be able to create a template with the name single-post-relief-fund.php that displays inidividual posts from that category. To quote the docs (and I have highlighted it below):

    The single post template file is used to render a single post. WordPress uses the following path:

    single-{post-type}-{slug}.php – (Since 4.4) First, WordPress looks for a template for the specific post. For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
    single-{post-type}.php – If the post type is product, WordPress would look for single-product.php.
    single.php – WordPress then falls back to single.php.
    singular.php – Then it falls back to singular.php.
    index.php – Finally, as mentioned above, WordPress ultimately falls back to index.php.

    However, the more specialised one is being ignored and the single.php is being used instead. I have checked the DB that the post-type is indeed ‘post’ and it is. Am I missing something?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    no. That is for a custom post type, not a category. For a custom template for a POST in a CATEGORY, you should put some logic inside single.php and use the get_template_part() function to pull in a different template.

    Something like:

    
    $fetchcats = array('names');
    $cats = wp_get_post_categories( 0, $fetchcats );
    if (in_array( 'somecat', $cats) {
       get_template_part( 'somecat' );
       } else {
       get_template_part( 'post ');
       }

    This code is off the top of my head and may contain many errors. But the concept is correct.

    Thread Starter mmacneil

    (@mmacneil)

    Thanks Steve, I see what you are getting at. My next attempt was to create a custom post type as there are already custom post-types in the site with their corresponding template parts but I thought the doc description was implying something else. I thought it might be quite a common scenario to tweak how you show a different category for the vanilla post-type.

    I am new to WP and PHP ??

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    You’d think there’s be a place in the template hierarchy for it, but there’s not. ??

    Thread Starter mmacneil

    (@mmacneil)

    Yes, a couple of mistakes in the code but a few hours of pain later…

    					$fetchcats = array ( 'fields' => 'names' );
    					$cats = wp_get_post_categories( $post->ID, $fetchcats );
    					// echo $cats[0];
    					if (in_array( 'Relief Fund', $cats)) {
    						// Relief specific
    						get_template_part( 'template-parts/content', 'relief' );
    					} else {
    						// Generic 
    						get_template_part( 'template-parts/content', 'single' );
    				    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Single post template not being used’ is closed to new replies.