Reputation: 1929
I'm developing a website with wordpress and I'm using underscores
starter theme.
I have 2 different categories and I need to create single templates for each one.
I'm having problems with that because wordpress doesn't assume my custom single templates and I don't understand why.
I already tried to put the single template in root theme directory, tried single-cat-categoryname.php
or just single-categoryname.php
, create a folder single but nothing works.
How can I create custom single posts templates with underscores
?
Thank you
Upvotes: 1
Views: 564
Reputation: 151
Using Wordpress's (WP for short) {$type}_template
filters (see here) you can do a check for various scenarios for when WP is trying to lift post template files.
If I understand your situation correctly, you'll want to tap into the single_template
filter and do some validating to ensure that the WP post meets your criteria.
Here's a sample piece of code:
function override_single_template( $template ) {
global $post;
if ( has_category( "cat_name", $post ) ) {
// set $template to file location of custom `single` template
// NOTE: file name for template does not have to follow WP post template
// naming convention BUT is preferred
}
return $template;
}
add_filter( 'single_template', 'override_single_templates' );
Upvotes: 1
Reputation: 395
In Wordpress a custom template for a single category must be named
category-{category id}.php
category-{slug}.php
Wordpress templates's hierarchy is based in names and ids, you can learn more about this here: Template hierarchy
Upvotes: 0
Reputation: 422
You should name the templates like this: category-slug.php
in your root theme folder.
Just replace slug with the slug of the category you want.
Hope it helps.
Upvotes: 0
Reputation: 11
According to the template hierarchy you can create template for category or term but not on single post which contain category
Ref: Template Hierarchy
but you can create template page where you can choose template for each post
Ref:Page Templates
Upvotes: 0