1

子カテゴリと孫カテゴリに同じカテゴリ テンプレートを使用するために Wordpress で見つけた最良の方法は、stackoverflow にあります: https://stackoverflow.com/a/3117202/391929

しかし、サブカテゴリのみがそのテンプレートを使用し、基本の親カテゴリを使用しないようにする方法は?

次から、そのようなif行だけを編集してみました:

if (is_category(get_cat_id('projects')) || cat_is_ancestor_of(get_cat_id('projects'), get_query_var('cat')))

これに:

if (is_category(get_cat_id('projects')) || cat_is_ancestor_of(get_cat_id('projects'), get_query_var('cat')))

しかし、それは機能していません、なぜですか?

4

1 に答える 1

2

私は手続き的な方法でこれを解決することができました:

1 このスニペットのテーマ関数に追加:

add_action('template_redirect', 'load_category_tree_template');
function load_category_tree_template() {
        if (is_category() && !is_feed()) {
            // replace 'your-base-category-slug' with the slug of root category
            $base_tree_cat_id = get_cat_id('your-base-category-slug'); 
                // get current category id
            $catid = get_query_var('cat'); 

            if (is_category($base_tree_cat_id) || cat_is_ancestor_of($base_tree_cat_id, $catid)) {
                load_template(STYLESHEETPATH . '/category-your-base-category-slug.php');
                exit;
            }
        }
    }

次に、category-your-root-category-slug.php ファイルに、現在のカテゴリ slug に基づいて別のファイルを含めます。

<?php get_header(); ?>

<?php
if (is_category( )) {
  $cat = get_query_var('cat');
  $currentcat = get_category ($cat);
  $currentslug = $currentcat->slug;
 }
// if we are in base category we know it by the slug
if ($currentslug == 'your-base-category-slug'){
        // if we are load a file with the loop for the root category
    require_once('post-loop-caregory-tree-root.php'); 
}else{
        // otherwise we are not in the root, so show the loop for all other category in the tree
    require_once('post-loop-category-tree.php'); 
}
?>

<?php get_footer(); ?>

もっとうまくできることはわかっていますが、最終的には非常に簡単な解決策です。

于 2013-03-16T17:39:35.490 に答える