繁体   English   中英

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

[英]Display a custom taxonomy in Woocommerce single product pages

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

// 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 模板文件的副本添加到该文件夹​​中。

谁能帮我?

  1. 我必须编辑哪些 woo 模板文件?
  2. 我需要向这些文件添加什么代码才能将我的新分类法插入到产品页面中?

在此先感谢您的任何帮助。

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

在单个产品页面上的类别和标签元下方有一个钩子可用。 这将完成这项工作。

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

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

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

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

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

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 文件中。 测试和工作。

它给了我一个错误:

致命错误语法错误,第 7 行出现意外的“$taxonomy”(T_VARIABLE),需要“)”

在:如果(!taxonomy_exists(字符串$taxonomy))

我该如何解决? 自定义分类在我的单个产品页面的 product_meta 中不可见

感谢您的代码。 不幸的是,它并没有解决我的问题。 这是我收到的错误代码。 无法与站点通信以检查致命错误,因此 PHP 更改已恢复。 您需要通过其他方式上传您的 PHP 文件更改,例如使用 SFTP。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM