0
votes

I would like to display a custom taxonomy name with its URL everywhere that WooCommerce products appear:

  1. on Homepage (https://rockers.pl/) - I have the best selling and the newest products shortcode
  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. Many thanks for any help.

1

1 Answers

1
votes

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

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.