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

How to display custom taxonomy of the WooCommerce product?

我想在 WooCommerce 产品出现的任何地方显示自定义分类法名称及其 URL:

  1. 在主页上 (https://rockers.pl/) - 我有最畅销和最新的产品简码
  2. 在存档页面上
  3. 在单个产品页面上

我为艺术家创建了自定义产品分类:

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: ""

现在 - 在产品标题附近 - 我需要在其分类法中显示带有 URL 的艺术家姓名。非常感谢您的帮助。

存档页面:

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 }
}

您还可以使用 woocommerce_after_shop_loop_item_title 操作将术语标题放在产品标题之后

对于单个产品页面,您需要在将标题添加到下面的挂钩之前或之后添加您的操作。

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

因此,您可以将上面的相同函数挂接到 woocommerce_single_product_summary,但优先级为 4 或 6,而不是 5。