2012-05-24 38 views
0

我想排除Wordpress中使用數組的某些帖子。我的Wordpress數組有什麼問題?

<?php if ($post->ID != '443') { ?> 
    ... 
<?php } ?> 

我試圖創建一個我要刪除帖子的數組,並有這樣的:我可以這樣做成功刪除的職位之一

<?php $excluded = array('443', '479', '464', '589', '333', '296', '381', '405', '252', '301', '291', '306', '632', '634', '636', '313', '317', '389', '410', '417', '321'); 
if ($post->ID != $exclude) { ?> 
    ... 
<?php } ?> 

我一直在努力教自己PHP和我在陣列掙扎,有什麼幫助?

+0

不應該你的if語句使用$ excluded變量嗎? – jamesTheProgrammer

回答

2

比較數字的數量不工作,因爲你的第一個例子。

但即時通訊第二個比較一個數字(ID)與數組(數字列表)。

要檢查,羯羊的ID是你的 「排除」 列表中的一個元素,你將不得不使用in_array()

if (!in_array($post->ID,$exclude)){ 
... 
} 

問候,

STEFAN

+0

感謝斯特凡那完美的工作! –

0

你應該使用in_array測試是否值是一個數組

if (in_array($post->ID, $exclude)) { 

} 
0

的問題,您如果語句是你正在檢查postID!=整個數組,而不是如果一個值在與該值相匹配的數組中。

您想使用in_array函數。這看起來像這樣:

if (!in_array($post->ID, $exclude)