In Twitter a user asks,
Question! Can I used a custom category template for specific categories using conditional tags? #genesiswp #wordpress
— Melissa (@blushandjelly) September 16, 2016
This can be done using template_include
filter in WordPress.
Step 1
Create your custom template named say, template_custom-category.php in your child theme directory having the following:
<?php
// Force full width content
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
genesis();
You would need to place your custom code in between the opening PHP tag and closing genesis();
function. In the above example we are setting the width of content to be full i.e., no sidebar.
Step 2
Let’s apply the above template to “Category 1” and “Category 2” category archive pages. Add the following in child theme’s functions.php:
/**
* Template Redirect
* Use template_custom-category.php for specific category archives.
*/
add_filter( 'template_include', 'custom_category_template', 99 );
function custom_category_template( $template ) {
if ( is_category( array( 'category-1', 'category-2' ) ) ) {
$new_template = locate_template( array( 'template_custom-category.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
Replace category-1
and category-2
with the slugs of your desired categories.
That’s it. Now all the category archive pages in your Genesis site except the specified categories will use the default layout (content, content/sidebar, sidebar/content etc.) as set in the Genesis theme settings while the specified categories will use full content layout.
Note: When you edit the specified categories in the back end and apply a layout other than the default layout, then it will be used for the display trumping the code from Step 2.
Thank you so much for this tutorial! Its already been so useful and saved me so much time (and the need of any extra plugins!).
Thanks, this was very educational.
What if I want a custom single page based on specific categories?
Try using
in_category
instead ofis_category
.http://developer.wordpress.org/reference/functions/in_category/
Nice Genesis Customisation it makes the site stand out of the crowd.
Thanks for your sharing. Another question, if I want to apply for all categories and not use a template is there a way?
Just create a template (file) named
category.php
.is there a way we can do this for post single? I have two categories in post (blog and news). so for blog, I want to use the default from genesis. and for news I want to use a custom template. badly need help thanks!
Code in
category-news.php
will only be applied for yournews
category’s archive page.https://developer.wordpress.org/themes/basics/template-hierarchy/#visual-overview
I meant the post page view for each category. but I got it now. I found the answer on the link you provided. thanks for the help!