php - How to write a function for a separate post template that will be applied by category? Wordpress -
good afternoon, there such task. there 2 categories, , posts of these 2 categories 1 post template should used. use code, not fit, have create separate template each category. (and need have 1 template).
add_filter('single_template', 'check_for_category_single_template'); function check_for_category_single_template( $t ){ foreach( (array) get_the_category() $cat ){ if ( file_exists(templatepath . "/single-category-{$cat->slug}.php") ) return templatepath . "/single-category-{$cat->slug}.php"; if($cat->parent){ $cat = get_the_category_by_id( $cat->parent ); if ( file_exists(templatepath . "/single-category-{$cat->slug}.php") ) return templatepath . "/single-category-{$cat->slug}.php"; } } return $t; }
this function changes below whould work.
i assume have templatepath defined.
also need go slides , pipe category , slugs 2 categories.
if different 'slides' , 'pipe' make changes below them match.
the template create must have name: single-category-slide_or_pipe.php
add_filter('single_template', 'check_for_category_single_template'); function check_for_category_single_template( $t ){ foreach( (array) get_the_category() $cat ){ if($cat->slug === 'slides' || $cat->slug === 'pipe'){ if ( file_exists(templatepath . "/single-category-slide_or_pipe.php") ){ return templatepath . "/single-category-slide_or_pipe.php"; } } } return $t; }
Comments
Post a Comment