0

我有一个代码,可与简码一起使用来获取父类别标题。当我在父类别时,我得到了正确的标题。当我在子类别中时,父类别标题不显示,而不是标题,我看到父类别 ID。你知道我该如何解决这个问题吗?

/*get title*/
function woocommerce_category_title() {
    if ( is_product_category() ){
        $term      = get_queried_object();
        $term_title   = $term->parent > 0 ? $term->parent : $term->name;
            echo $term_title;
        }
    
}
add_shortcode( 'cattit', 'woocommerce_category_title' );

先感谢您。

4

1 回答 1

1
function woocommerce_category_title() {
    if ( is_product_category() ){
        $term      = get_queried_object();
        $term_title   = $term->parent > 0 ? $term->parent : $term->name;
        if($term->parent > 0) {
        $parent_term = get_term_by('ID', $term->parent, 'product_cat');
        $term_title = $parent_term->name;
        } else {
        $term_title = $term->name;
        }
            echo $term_title;
        }
    
}
add_shortcode( 'cattit', 'woocommerce_category_title' );
于 2021-09-21T11:45:35.597 回答