Skip to content

Instantly share code, notes, and snippets.

@chocotaro
Created August 27, 2019 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chocotaro/b12d71e5894888f7c2a3475fd4afeb09 to your computer and use it in GitHub Desktop.
Save chocotaro/b12d71e5894888f7c2a3475fd4afeb09 to your computer and use it in GitHub Desktop.
親カテゴリのテンプレートを子カテゴリに反映
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