How to load posts from a specific wordpress category in a separate template?

Asked by Anonymous on 14 Feb 2019 1508 Views

Jiwan on 14 Feb 2019


Wordpress follows it's own templates heirarchy while loading templates for a spicific content like home page, single page, category, tags, taxonomies, custom post types, author display, search results, or any other content. Category post also follows the same rule.

Learn more about wordpress template heirarchy from here. Wordpress Template Heirarchy

By default, wordpress posts are loaded in a template named single.php while other templates on the heirarchy are as follows.

Single Post

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, WordPress ultimately falls back to index.php.

As stated above, we can define a separate template for a specific post by naming a template as single-post-type-post-slug or change their post type to a specific post format and load them in a template named single-post-type.

But, in most cases, we'll need a solution to load posts from the regular post formats of a specific category in a separate template and here's what you can do to load posts from a specific wordpress category in a separate template.

Method I : Easy

This is the easiest method to load posts from a specific category into a separate template. Simply, add the following code on top of the template named single.php. Replace the term blog with your category name and the filename in the get_template_part() function with your template filename where you want to load the posts you wanted and see the magic.

<?php        
if(has_term('blog', 'category', $post)) { 
	get_template_part('single-blog', 'blog');
}
// Change the category term and template name with your category and template name
?>

Method II : Standard

Though the first method is the easiest one, this method is the standard one used by most of the developers which helps you to keep your template file clean. Simply, add this block of code in your functions.php file while replacing the category id with the one you want to and the template name with your template name.

That's all you need to do to load posts from a specific wordpress category in a separate template.

add_filter( 'single_template', function ( $single_template ) {

    $parent     = '16'; //Change category ID to your category ID
    $categories = get_categories( 'child_of=' . $parent );
    $cat_names  = wp_list_pluck( $categories, 'name' );

    if ( has_category( 'blog' ) || has_category( $cat_names ) ) {
        $single_template = 'single-blog.php'; // Change template name to your template name
    }
    return $single_template;

}, PHP_INT_MAX, 2 );

Hope, this answer helped you. Let me know through the comments.


You must be logged in to answer a question. or