【问题标题】:Add a product tag to product name in Woocommerce archive pages在 Woocommerce 存档页面中为产品名称添加产品标签
【发布时间】:2018-03-28 01:39:17
【问题描述】:

我正在尝试在 Woocommerce 上的产品名称下方添加产品标签。我在这里找到了类似的代码并尝试对其进行自定义,但无法正常工作。

/**
 * Add product's brand name to product's name.
 */
add_filter( 'the_title', 'mycode_add_brand_to_product_title', 10, 2 );
function mycode_add_brand_to_product_title( $title, $id ) {
  $type = get_post_type( $id ); 
  if ( $type != 'product' ) { return $title; }
  $terms = wc_get_product_terms( $post->ID, 'product_tag' );
  $brand = array_shift( $terms );
  if ( !empty( $brand ) ) {
    $title = $brand . ' ' . $title;
  }
  return $title;
}

有人可以帮忙吗?

这是我想要实现的预览:

【问题讨论】:

    标签: php wordpress woocommerce product custom-taxonomy


    【解决方案1】:

    更新:尝试以下操作:

    add_filter( 'the_title', 'mycode_add_brand_to_product_title', 10, 2 );
    function mycode_add_brand_to_product_title( $title, $post_id ) {
    
        if ( get_post_type( $post_id ) != 'product' && ! is_archive() )
            return $title;
    
        if ( ! ( is_shop() || is_product_category() || is_product_tag() ) )
            return $title;
    
        $terms = wp_get_post_terms( $post_id, 'product_tag' );
        $term = reset( $terms );
    
        if ( !empty( $term->name ) )
            $title .= '<br><small>' . strtoupper( $term->name ) . '</small>';
    
        return $title;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。
    它也应该适合你。

    【讨论】:

    • 抱歉,还有一件事?如何仅在存档页面上显示此内容?因为现在它无处不在!
    猜你喜欢
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多