2013-04-03 117 views
0

我有以下基於常規WordPress自定義字段名稱的GET功能。 當打勾時,它將所有具有該自定義字段值設置爲1的帖子排序。 它當前有效。但是,我碰巧有兩個自定義字段:'free'和'twofree'元名稱不區分大小寫

當我勾選'free'時,它還包含'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'); 

回答

2

使用身份操作時要匹配精確值===,而不是==,它根據字符串/整數檢查相似的值。


更多一個這個問題,看看這個link或這個答案下面

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 
+0

這有什麼好做的問題。這也是'=='運算符所做的不正確描述。在比較值之前'=='將執行_type coercion_(不轉換)。這意味着'false'和'0'是'==',因爲它們在相同類型時相同。然而,==='尊重類型並且沒有這樣的強制。因此,錯誤的'!=='0(因爲一個是布爾值,一個是整數)。兩者都不會使'free'=='twofree''也不會改變情況 –

+0

也許我不明白他的問題,但他的頭銜似乎表明他正試圖比較兩個值,這兩個值是區分大小寫的但在使用==運算符 – samayo

+0

'=='進行計算時,計算出的值相同時不會在比較時轉換大小寫。 「這個」'!='「這個」。它只在比較之前執行類型強制。沒有其他的。他的代碼不是問題。 MySQL中的數據庫值(我認爲是支持這個)不區分大小寫。這可能是WP –