需要一些帮助来调整WooCommerce的代码

时间:2014-03-02 作者:doob

我有这个代码来显示我选择的类别中的自定义文本。

<?php if (is_product_category(\'furnitures\')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_product_category(\'kitchen\')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages, 
I could be left blank</p>
<?php endif; ?>
如何使文本也显示在每个父级的子类别中。在上面的代码中,“家具”和“厨房”是父类,并且有一些子类别。

谢谢

1 个回复
最合适的回答,由SO网友:Ravinder Kumar 整理而成

try this

$term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) ); // current term
$parent_slug = \'\'; // default slug param

if( $term->parent ){   // if current term has parent then get it\'s slug
   $parent = get_term( $term->parent, get_query_var(\'taxonomy\') ); // term\'s parent
   $parent_slug = $parent->slug; // slug of parent term
}

// check current term has product_cat taxonomy
$is_product_category = \'\' === $parent_slug ? 0: is_product_category($term->slug);

if ( is_product_category(\'furnitures\') || ( $is_product_category  && \'furnitures\' === $parent_slug) ) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_product_category(\'kitchen\') || ( $is_product_category && \'kitchen\' === $parent_slug) ) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages, 
I could be left blank</p>
<?php endif; ?>
结束