简体   繁体   English

如何显示WooCommerce产品的自定义分类法?

[英]How to display custom taxonomy of the WooCommerce product?

I would like to display a custom taxonomy name with its URL everywhere that WooCommerce products appear: 我想在WooCommerce产品出现的所有地方显示自定义分类名称及其URL:

  1. on Homepage ( https://rockers.pl/ ) - I have the best selling and the newest products shortcode 在主页( https://rockers.pl/ )上-我有最畅销和最新的产品短代码
  2. on Archive pages 在存档页面上
  3. on Single Product page 在单一产品页面上

I created custom product taxonomy for the artist: 我为艺术家创建了自定义产品分类法:

name: artysci
label: Artyści
singular_label: Artysta
description: ""
public: true
publicly_queryable: true
hierarchical: true
show_ui: true
show_in_menu: true
show_in_nav_menus: true
query_var: true
query_var_slug: ""
rewrite: true
rewrite_slug: ""
rewrite_withfront: true
rewrite_hierarchical: true
show_admin_column: true
show_in_rest: true
show_in_quick_edit: ""
rest_base: ""
rest_controller_class: ""
meta_box_cb: ""

and now - near the title of the product - I need to display the artist name with URL to its taxonomy. 现在-在产品标题附近-我需要显示艺术家名称以及其分类法的URL。 Many thanks for any help. 非常感谢您的帮助。

For archive pages: 对于存档页面:

add_action( 'woocommerce_before_shop_loop_item_title', 'add_artist_term');
function add_artist_term() {
    $terms = wp_get_post_terms(get_the_ID(), 'artysci');
    if (!is_wp_error($terms) && !empty($terms)) { ?>
        <div class="artist-term-title"><a href="<?php echo esc_url(get_term_link($terms[0])); ?>"><?php echo esc_html($terms[0]->name); ?></a></div>
    <?php }
}

You could also use the woocommerce_after_shop_loop_item_title action to put the term title after the product title 您也可以使用woocommerce_after_shop_loop_item_title操作将术语标题放在产品标题之后

For single product page you need to add your action either before or after the title is added to the hook below. 对于单个产品页面,您需要在标题添加到下面的挂钩之前或之后添加操作。

add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );

So you would hook the same function from above onto woocommerce_single_product_summary but with a priority of 4 or 6 instead of 5. 因此,您可以从上方将相同的函数挂接到woocommerce_single_product_summary但优先级为4或6而不是5。

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

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