在Woocommerce单一产品页面中显示自定义分类法 [英] Display a custom taxonomy in Woocommerce single product pages

查看:396
本文介绍了在Woocommerce单一产品页面中显示自定义分类法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用以下代码向Woocommerce添加了一个新的分类法,称为供应商:

I have added a new taxonomy called "Vendor" to Woocommerce with the following code:

// 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 );

}

我想在类别和标签之间插入新的分类法标签出现在单个产品页面上。

I want to insert this new taxonomy between the Category and Tags labels that appear on a single product page.

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

I have a child theme and understand that I must create a woocommerce folder in the child and then add to that folder a copy of the woo template files that I must edit.

有人可以帮助我吗?


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

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

预先感谢任何形式的帮助。

Thanks in advance for any kind assistance.

更新:
经过进一步的研究,看来我不需要编辑Woo模板文件。

UPDATE: Upon further research it would appear that I do not need to edit the Woo template files.

单个产品页面上的类别和标签元。这样便可以完成工作。

There is a hook available just below the Category and Tags meta on the single product page. That will do the job.

因此,我可以使用以下内容插入供应商分类法详细信息:

So I can insert the Vendor taxonomy details with the following:

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?];
}

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

Thanks to anyone who can help me out.

推荐答案

要在Woocommerce单个产品页面的meta部分中显示自定义分类术语中的发布术语,则无需覆盖任何Woocommerce模板。

To display the post terms from a custom taxonomy terms in meta section of Woocommerce single product page, you don't need to override any Woocommerce template.

相反,您可以这样使用特定的专用操作钩子:

Instead you can use the specific dedicated action hook this way:

add_action( 'woocommerce_product_meta_end', 'action_product_meta_end' );
function action_product_meta_end() {
    global $product;

    $taxonomy = 'vendor'; // <== Here set your custom taxonomy

    if( ! taxonomy_exists( string $taxonomy ) ) 
        return; // exit
    
    $term_ids = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'ids') );

    if ( ! empty($term_ids) ) {
        echo get_the_term_list( $product->get_id(), 'vendor', '<span class="posted_in">' . _n( 'Vendor:', 'Vendors:', count( $term_ids ), 'woocommerce' ) . ' ', ', ', '</span>' );
    }
}

活动子主题的function.php文件中包含代码(或活动主题)。经过测试并可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在Woocommerce单一产品页面中显示自定义分类法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆