代码之家  ›  专栏  ›  技术社区  ›  nsek

在Woocommerce产品标题之前显示分类

  •  0
  • nsek  · 技术社区  · 3 年前

    我已经使用以下片段将分类法“product_tag”重命名为“artist”,我希望它显示在单个产品页面和存档页面中。

    function change_cat_object() {
        global $wp_taxonomies;
        $labels = &$wp_taxonomies['product_tag']->labels;
        $labels->name = 'Artist';
        $labels->singular_name = 'Artist';
        $labels->add_new = 'Add Artist';
        $labels->add_new_item = 'Add Artist';
        $labels->edit_item = 'Edit Artist';
        $labels->new_item = 'Artist';
        $labels->view_item = 'View Artist';
        $labels->search_items = 'Search Artists';
        $labels->not_found = 'No Artists found';
        $labels->not_found_in_trash = 'No Artists found in Trash';
        $labels->all_items = 'All Artists';
        $labels->menu_name = 'Artist';
        $labels->name_admin_bar = 'Artist';
    }
    add_action( 'init', 'change_cat_object' );
    

    在单一产品页面中,我使用了以下代码,并且运行良好:

    add_action( 'woocommerce_single_product_summary', 'custom_add_product_cat', 0 );
    function custom_add_product_cat(){
        global $product;
        $product_cats = wp_get_post_terms($product->id, 'product_tag');
        $count = count($product_cats);
        $categories = '';
        foreach($product_cats as $key => $cat){
            $categories .= $cat->name;
            if($key < ($count-1)){
                $categories .= ', ';
            }
        }
    
        echo '<div class="artist-name">'. $categories . '</div>';
    }
    

    在归档页面中,产品标题和价格是产品图像的叠加。我使用的是'woococommerce_before_shop_lop_item_title',但分类法没有显示;如果使用'woococommerce_shop_lop_item_title',则会显示分类法,但位于产品标题和产品价格之间。

    add_action( 'woocommerce_shop_loop_item_title', 'custom_add_product_tag', 0 );
    function custom_add_product_tag(){
        global $product;
        $product_tags = wp_get_post_terms($product->id, 'product_tag');
        $count = count($product_tags);
        $categories = '';
        foreach($product_tags as $key => $tag){
            $categories .= $tag->name;
            if($key < ($count-1)){
                $categories .= ', ';
            }
        }
    
        echo '<div class="artist-tag">'. $categories . '</div>';
    }
    
    

    在存档页面中的产品图像下方,其中“RING RING”是产品标题,“ABBA”是分类法:

    enter image description here

    如有任何帮助,我们将不胜感激。

    0 回复  |  直到 3 年前