【问题标题】:show custom field value of product on archive page在存档页面上显示产品的自定义字段值
【发布时间】:2020-07-14 10:57:05
【问题描述】:

我想显示在自定义字段中设置的值以显示在购物循环项目中。它适用于商店并显示基于产品的自定义字段。但是产品页面本身的项目显示了产品页面的所有价值,而不是单个产品。我该如何修改代码才能正常工作?

add_action('woocommerce_after_shop_loop_item', 'grundpreis');
function grundpreis () {
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'grund-preis', true);
    wp_reset_query();
}

它在这里工作:https://keimster.de/shop/ 但不在这里:https://keimster.de/produkt/vier-koerner-keimster

【问题讨论】:

    标签: php woocommerce custom-fields


    【解决方案1】:

    你能试试下面的代码吗?我相信问题出在$wp_query

    function grundpreis () {
        global $post;
    
        // Check for the custom field value
        $my_field = get_post_meta($post->ID, 'grund-preis', true);
    
        if ( $my_field ) {
            echo $my_field;
        }
    }
    add_action('woocommerce_after_shop_loop_item', 'grundpreis', 10, 0 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-24
      • 2019-01-09
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多