【问题标题】:Custom fields woocommerce自定义字段 woocommerce
【发布时间】:2016-09-30 10:46:28
【问题描述】:

我在 woocommerce 单品页面上显示一些自定义字段

add_action( 'woocommerce_single_product_summary','add_custom_field', 20 );
function add_custom_field() {
   global $post;
   echo get_post_meta( $post->ID, 'Brand', true );
   echo get_post_meta( $post->ID, 'Content', true );
   return true;
}

这仅显示自定义字段的值,但我想要之前的名称,所以它看起来像这样:

品牌:...
内容:...

自定义字段并不适用于所有产品,因此对于未设置自定义字段的产品,不应显示任何内容。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    使用这个:

    add_action( 'woocommerce_single_product_summary', 'add_custom_field', 20 );
    function add_custom_field() {
        global $post;
        $brand =   get_post_meta( $post->ID, 'Brand', true );
        $content = get_post_meta( $post->ID, 'Content', true );
        if (!empty($brand)) {
            echo 'Brand: '. $brand;
        }
        if (!empty($content)) {
            echo 'Content: '. $content;
        }
    }
    

    【讨论】:

    • 谢谢,这行得通,但是两个自定义字段是直接在彼此之后显示的。我想将它们放在单独的一行中,可以以某种方式添加
      吗?
    • 是的,您可以在 $brand 之后添加
      ,就像这样 echo 'Brand:'。 $品牌。 '
      ';
    【解决方案2】:

    试试下面的代码

    add_action( 'woocommerce_single_product_summary', 'add_custom_field', 20 );
    function add_custom_field() {
        global $post;
        $brand =   get_post_meta( $post->ID, 'Brand', true );
        $content = get_post_meta( $post->ID, 'Content', true );
        if (!empty($brand)) {
            echo 'Brand: '. $brand .'<br>';
        }
        if (!empty($content)) {
            echo 'Content: '. $content .'<br>';
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 2016-07-22
      • 2023-04-10
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多