简体   繁体   English

在 WooCommerce 产品标题之前显示自定义分类

[英]Show Custom Taxonomy Before WooCommerce Product title

I have created a custom taxonomy (shipment-status) for WooCommerce products having two terms ie [Shipped from abroad & Available now]我为 WooCommerce 产品创建了一个自定义分类法(发货状态) ,有两个术语,即[从国外发货 & 现在可用]

Only one term is selected per product.每个产品只选择一个术语。 I will like to show the selected term above the WooCommerce product title on the single product page and on the archive card as seen in the images below.我想在单个产品页面和存档卡上的 WooCommerce 产品标题上方显示所选术语,如下图所示。 I would like to style the two terms differently, so will like to have a class attached to them.我想以不同的方式对这两个术语进行样式设置,因此希望为它们添加一个类。

Am currently using the snippet below to display it but how do i change the class based on the term name or id?目前正在使用下面的代码段来显示它,但我如何根据术语名称或 ID 更改类?

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

Thanks in advance for helping.提前感谢您的帮助。

在此处输入图片说明 在此处输入图片说明

There are some things to be aware of but you could just use the term's slug as a class in the div like this:有一些事情需要注意,但你可以像这样使用术语的 slug 作为 div 中的一个类:

add_action( 'woocommerce_before_shop_loop_item_title', 'add_artist_term');
function add_artist_term() {
    $terms = wp_get_post_terms(get_the_ID(), 'shipment-status');
    if (!is_wp_error($terms) && !empty($terms)) {
        $term = $terms[0];
        $class = esc_attr( $term->slug );
        $url = esc_url( get_term_link( $term ) );
        $name = esc_html( $term->name );
        echo "<div class=\"shipment-status $class\"><a href=\"$url\">$name</a></div>";
    }
}

You have to be aware that in general slugs are not always valid css class names.您必须注意,通常 slug 并不总是有效的 css 类名。 But since you create the taxonomy and you say that there are exactly two terms I assume you also create the terms and you do not allow users to add or delete them.但是由于您创建了分类法并且您说正好有两个术语,我假设您也创建了这些术语并且您不允许用户添加或删除它们。 In that case you can specify the slugs for those terms when you create them and safely use them as css class names.在这种情况下,您可以在创建这些术语时为这些术语指定 slug,并安全地将它们用作 css 类名。

Below is the snippet to show the taxonomy on the single product page.If anyone every needs it.下面是在单个产品页面上显示分类法的片段。如果有人需要的话。

add_action( 'woocommerce_single_product_summary', 'ship_status', 5);
function ship_status() {
    $terms = wp_get_post_terms(get_the_ID(), 'shipment-status');
    if (!is_wp_error($terms) && !empty($terms)) {
        $term = $terms[0];
        $class = esc_attr( $term->slug );
        $url = esc_url( get_term_link( $term ) );
        $name = esc_html( $term->name );
        echo "<div class=\"shipment-status $class\"><a href=\"$url\">$name</a></div>";
    }
}

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

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