duanliang9288 2015-07-14 14:08 采纳率: 100%
浏览 11
已采纳

如果meta_data <= 50

I'm trying to automate the delivery (free above 50) message in my template. Currently I'm echo'ing the normal price & discount price (if available) which results in digits.

I have a post with Normal_price = 55 / Discount_price = 38

I'm new to PHP but tried the following:

<?php $discount = (get_post_meta($post->ID, 'Discount_price', true));
      $normal = (get_post_meta($post->ID, 'Normal_price', true));

     if $discount = (>= 50) {
     echo 'under 50 euro';
     }

     else if $normal = (>= 50) {
     echo 'under 50 euro';
     }

     else
    {
    echo 'above 50 euro';
    }



?> 

How do I declare $discount = post_meta 'Discount_Price'?

  • 写回答

1条回答 默认 最新

  • doujieluo5875 2015-07-14 14:59
    关注

    I think what you really want to do is:

    $discountPrice = (get_post_meta(get_the_ID(), 'Discount_price', true));
    $normalPrice = (get_post_meta(get_the_ID(), 'Normal_price', true));
    // NOTE: get_the_ID() will only work if you are inside the loop
    
    if ( $discountPrice <= 50 ) {
        // 50 euro or less
        echo '50 euro or less';
    } else if ( $normalPrice <= 50 ) {
        // 50 euro or less
        echo '50 euro or less';
    } else {
        // over 50 euro
        echo 'over 50 euro';
    }
    

    You've got your operators reversed on your compares, which is part of the problem, but also $discount = (>=50) will not work. As one of the comments mentioned, that's not the correct syntax.

    It won't solve the problem, but just as an FYI: Using a single = sets a value. Using == compares a value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大