Display WP Posts With Custom Templates By Category — WordPress Tutorials
In order to Display WP Posts With Custom Templates By Category we are attempting to assign a category to a WP post, and filter the posts by category in order to assign a custom template to posts of specific categories.
This is relatively easy to do in WordPress Development (but not as easy to describe, as it took me numerous wordings to get the information I wanted). Essentially what we are trying to do is to tell WP when loading a post to check the categories of that post, and we want certain categories to point away from single.php (which is what posts use by default) to something specific of our choosing (single-videos.php, etc).
single posts from different categories using differnt single.php template in WordPress, you can use the single_template filter that is built in to WP.
Create Custom Template
The first step is to Create A Custom Template to display the information of this category. This is where we are going to do our custom HTML, CSS, JS, PHP to display the information in different ways.
Perhaps this post category is designed for videos, so we want the embedded media to appear in a large box, with the post text itself in its own box beneath the video. For example. We can do this so that every post that is assigned the ‘Video’ category will display with this custom template.
Copy your single.php and rename it to something of your choosing, in the form single-yourcategory.php. This is for organization, and also so that WordPress knows
Implement single_template Filter
Next we are going to implement the single_template filter.
Open the themes functions.php page. Add the code:
// Assign Single Template By Category
function custom_single_template_by_category( $template ) {
global $post;
if ( is_single() && has_category( 'ghk', $post ) ) {
$new_template = locate_template( array('single-ghk.php') );
if ( '' != $new_template ) {
return $new_template;
}
}
if ( is_single() && has_category('atlas', $post) ) {
$custom_template = locate_template( array('single-atlas.php') );
if ( '' != $custom_template ) {
return $custom_template;
}
}
return $template; // Return default single.php if no template is specified.
}
add_filter( 'single_template', 'custom_single_template_by_category');This code will dynamically assign correct template based on posts category.
You will notice that I added more conditions, because I want more categories to point to unique post and page Templates.
This also works with Custom Post Types that point to the same single-custom.php.
Explanation
- The custom_single_template_by_category function is hooked into the single_template filter, which allows you to modify the template path for single posts.
- is_single() checks if the current page is a single post.
- has_category( ‘restaurants’, $post ) checks if the current post belongs to the category with the slug “article” Replace “restaurants” with your desired category slug.
- locate_template() finds the specified template file within your theme.
- If a specific template is found, its path is returned, overriding the default single.php.
- If no specific template is found for the category, the original $template (which would be single.php) is returned.
Assign Categories To Your Posts
Ensure that your posts are correctly assigned to the categories for which you have created custom single.php templates. You can do this in the WordPress post editor under the “Categories” meta box.