首页 文章

在Woocommerce单个产品页面中显示自定义分类

提问于
浏览
1

我使用以下代码向Woocommerce添加了一个名为“Vendor”的新分类:

// hook into the init action and call taxonomy when it fires

add_action( 'init', 'create_vendor_taxonomy', 0 );

// create and register vendor taxonomy (hierarchical)

function create_vendor_taxonomy() {

    $labels = array(
        'name'              => _x( 'Vendors', 'taxonomy general name', 'textdomain' ),
        'singular_name'     => _x( 'Vendor', 'taxonomy singular name', 'textdomain' ),
        'search_items'      => __( 'Search Vendors', 'textdomain' ),
        'all_items'         => __( 'All Vendors', 'textdomain' ),
        'parent_item'       => __( 'Parent Vendor', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Vendor:', 'textdomain' ),
        'edit_item'         => __( 'Edit Vendor', 'textdomain' ),
        'update_item'       => __( 'Update Vendor', 'textdomain' ),
        'add_new_item'      => __( 'Add New Vendor', 'textdomain' ),
        'new_item_name'     => __( 'New Vendor Name', 'textdomain' ),
        'menu_name'         => __( 'Vendors', 'textdomain' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'vendor' ),
    );

    register_taxonomy( 'vendor', array( 'product' ), $args );

}

我想在单个产品页面上显示的类别和标签标签之间插入此新分类 .

我有一个孩子主题,并了解我必须在孩子中创建一个woocommerce文件夹,然后添加我必须编辑的woo模板文件的副本 .

谁能帮我?

  • 我必须编辑哪些woo模板文件?

  • 我需要在这些文件中添加哪些代码才能将新的分类法插入产品页面?

在此先感谢您提供任何帮助 .

UPDATE: 经过进一步研究,我似乎不需要编辑Woo模板文件 .

单个产品页面上的Category和Tags元下方有一个钩子 . 那将完成这项工作 .

所以我可以使用以下内容插入Vendor分类法详细信息:

add_action( 'woocommerce_product_meta_end', 'insert_vendor_custom_action', 5 );

function insert_vendor_custom_action() {
    global $product;
    if [WHAT DO I NEED HERE?]
    echo [WHAT DO I NEED HERE?];
}

感谢任何可以帮助我的人 .

1 回答

  • 2

    要在Woocommerce单一产品页面的元部分中显示自定义分类术语中的帖子术语,您无需覆盖任何Woocommerce模板 .

    相反,您可以通过这种方式使用特定的专用操作挂钩:

    add_action( 'woocommerce_product_meta_end', 'action_product_meta_end' );
    function action_product_meta_end() {
        global $product;
    
        $term_ids = wp_get_post_terms( $product->get_id(), 'vendor', array('fields' => 'ids') );
    
        echo get_the_term_list( $product->get_id(), 'vendor', '<span class="posted_in">' . _n( 'Vendor:', 'Vendors:', count( $term_ids ), 'woocommerce' ) . ' ', ', ', '</span>' );
    }
    

    代码位于活动子主题(或活动主题)的function.php文件中 . 经过测试和工作 .

相关问题