繁体   English   中英

在 WooCommerce 商店页面上显示自定义分类术语

[英]Display custom taxonomy terms on WooCommerce shop page

我正在研究如何在 WooCommerce 商店循环中显示自定义分类法。

我找到了这个答案,它为我指明了正确的方向。 我已将该答案的代码修改为以下内容:

add_action( 'woocommerce_after_shop_loop_item_title', 'action_product_meta_end' );
function action_product_meta_end() {
    global $product;

    $taxonomy = 'keyfeatures'; // <== Here set your custom taxonomy

   if( ! is_taxonomy( $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(), 'keyfeatures', '<br /><span class="posted_in">' . _n( 'Key Feature:', 'Key Features:', count( $term_ids ), 'woocommerce' ) . ' ', ', ', '</span>' );
    }
}

我坚持的部分是这一行:

echo get_the_term_list( $product->get_id(), 'keyfeatures', '<br /><span class="posted_in">' . _n( 'Feature:', 'Features:', count( $term_ids ), 'woocommerce' ) . ' ', ', ', '</span>' );

我将 was VendorVendors (分类法的名称)更改为FeatureFeatures 但我实际上想完全删除它。

我想以下列格式输出自定义分类术语:

Term1 | Term2 | Term3

上面的行将它们输出为:

Features: Term1, Term2, Term3

我还需要在 output 周围添加一个<span></span> ,这样我就可以用 CSS 设置样式。

我需要做哪些更改才能获得所需的 output? (只是分类术语,用 pipe |分隔,没有别的?

###Update 在评论中,swadhwa 建议我查看此页面,这正是我需要查看的内容。

根据该页面上的信息,我将(输出)代码行更改为:

echo get_the_term_list( $product->get_id(), 'keyfeatures', '<span class="mks_prod_keyfeatures">', ' | ', '</span>' );

然而,奇怪的是,来自 Wordpress 的 output 将<span class="mks_prod_keyfeatures"'></span>放入我的分类法 output 上方的<a...></a>中。所以我还必须更改WC 从woocommerce_after_shop_loop_item_title挂钩到woocommerce_after_shop_loop_item 这给出了预期的结果。

你希望你的 output 是这样的:

Term1 | Term2 | Term3

那么这应该有所帮助:

get_the_term_list( $product->get_id(), 'keyfeatures', '<span class="posted_in">', '|', '</span>' )

其中第三个参数是之前的内容,第四个是分隔符,第五个是之后的内容。

暂无
暂无

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

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