web-dev-qa-db-ja.com

すべてのサブカテゴリでそのカテゴリの親のテンプレートを使用するようにしますか。

すべてのサブカテゴリにそのカテゴリの親のテンプレートを使用させるにはどうすればよいですか。

4
Adam

これを実行するために使用したコードは次のとおりです。

// make category use parent category template
function load_cat_parent_template($template) {

    $cat_ID = absint( get_query_var('cat') );
    $category = get_category( $cat_ID );

    $templates = array();

    if ( !is_wp_error($category) )
        $templates[] = "category-{$category->slug}.php";

    $templates[] = "category-$cat_ID.php";

    // trace back the parent hierarchy and locate a template
    if ( !is_wp_error($category) ) {
        $category = $category->parent ? get_category($category->parent) : '';

        if( !empty($category) ) {
            if ( !is_wp_error($category) )
                $templates[] = "category-{$category->slug}.php";

            $templates[] = "category-{$category->term_id}.php";
        }
    }

    $templates[] = "category.php";
    $template = locate_template($templates);

    return $template;
}
add_action('category_template', 'load_cat_parent_template');
6
sorich87

Daniel Crabbeにも同じ問題がありました。 Found このサイト 適切な投稿を表示しながら、親カテゴリのテンプレートファイルを探して使用するコードがあります。

ファイル名のスラッグをサポートするように更新しました(カテゴリIDだけではありません)。

// Use a parent category slug if it exists
function child_force_category_template($template) {
    $cat = get_query_var('cat');
    $category = get_category($cat);

    if ( file_exists(TEMPLATEPATH . '/category-' . $category->cat_ID . '.php') ) {
        $cat_template = TEMPLATEPATH . '/category-' . $category ->cat_ID . '.php';
    } elseif ( file_exists(TEMPLATEPATH . '/category-' . $category->slug . '.php') ) {
        $cat_template = TEMPLATEPATH . '/category-' . $category ->slug . '.php';
    } elseif ( file_exists(TEMPLATEPATH . '/category-' . $category->category_parent . '.php') ) {
        $cat_template = TEMPLATEPATH . '/category-' . $category->category_parent . '.php';
    } else {
        // Get Parent Slug
        $cat_parent = get_category($category->category_parent);

        if ( file_exists(TEMPLATEPATH . '/category-' . $cat_parent->slug . '.php') ) {
            $cat_template = TEMPLATEPATH . '/category-' . $cat_parent->slug . '.php';
        } else {
            $cat_template = $template;
        }

    }

    return $cat_template;
}
add_action('category_template', 'child_force_category_template');
3
Eric B

実はそれはフィルターフックです

https://developer.wordpress.org/reference/functions/get_category_template/ /

/**
 * Retrieve path of category-videos.php template for 'videos' subcategories
 * Filtered via 'category_template' hook
 *
 * @param string Full path to category template file.
 *
 * @return string Full path to category template file.
 */
function get_category_videos_template( $template ) {
  $category_ID = intval( get_query_var('cat') );
  $category = get_category( $category_ID );
  $parent_ID = $category->parent;
  $parent_category = get_category( $parent_ID );
  /**
   * Check wheter the $parent_category object is not a WordPress Error and its slug is 'videos'
   */
  if ( !is_wp_error( $parent_category ) AND $parent_category->slug == 'videos' ) {
    /**
     * Check if the template file exists
     *
     */
    if ( file_exists( TEMPLATEPATH . '/category-' . $parent_category->slug . '.php' )) {
      return TEMPLATEPATH . '/category-' . $parent_category->slug . '.php';
    }
  } else {
    return $template;
  }
}
add_filter( 'category_template', 'get_category_videos_template' );
0
montalvomiguelo