Skip to content

Instantly share code, notes, and snippets.

@chocotaro chocotaro/category_template Secret
Created Aug 27, 2019

Embed
What would you like to do?
親カテゴリのテンプレートを子カテゴリに反映
add_filter( 'category_template', 'my_category_template' );
function my_category_template( $template ) {
$category = get_queried_object();
if ( $category->parent != 0 &&
( $template == "" || strpos( $template, "category.php" ) !== false ) ) {
$templates = array();
while ( $category->parent ) {
$category = get_category( $category->parent );
if ( !isset( $category->slug ) ) break;
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
}
$templates[] = "category.php";
$template = locate_template( $templates );
}
return $template;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.