0

我有以下基于常规 WordPress 自定义字段名称的 GET 函数。勾选后,它将对该自定义字段值设置为 1 的所有帖子进行排序。它当前有效。但是,我碰巧有两个名为“free”和“twofree”的自定义字段

当我勾选“免费”时,它还包括“twofree”,反之亦然。它似乎不区分大小写。有什么解决方法吗?

<?php 
/* my code starts from  here*/
 if( isset( $_GET['show'] ) && !empty ( $_GET['show']  )  ){
   if( $_GET['show'] == 1 ) 
    $meta_field = 'free';
  else  if( $_GET['show'] == 4 )
    $meta_field = 'sale';
   else if( $_GET['show'] == 2 )
    $meta_field = 'genuine';
    else if ( $_GET['show'] == 'onfire' )
    $meta_field = 'onfire';
    else if( $_GET['show'] == 5 )
    $meta_field = 'twofree';    
 else if( $_GET['show'] == 3 )
    $meta_field = 'onfire'; 

        if( $_GET['show'] == 'sale' )
            query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value > 0');
        else
          query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value>=1');   
 }
/* my code ends from  here*/ 
?>

编辑:我发现了问题,它在于部分

query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value>=1');

我把它改成

query_posts('cat='.$cat.'&meta_key='.$meta_field.'&meta_value=1');
4

1 回答 1

2

===当您想要匹配精确值时使用身份运算符,而不是==根据字符串/整数检查相似值。


更多此主题,请查看此链接或下面的此答案

Using the `==` operator (*Equality*)

    true == 1; //true, because 'true' is converted to 1 and then compared
    "2" == 2 //true, because 2 is converted to "2" and then compared

Using the `===` operator (*Identity*)

    true === 1 //false
    "2" === 2 // false

This is because the **equality operator == does type coercion**...meaning that the interpreter implicitly tries to convert the values and then does the comparing.

On the other hand, the **identity operator === does not do type coercion**, and so thus it does not convert the values of the values when comparing
于 2013-04-03T13:31:55.317 回答