2014-03-26 57 views
0

不同爲了檢查一個文章ID存在,我這樣做:如何檢查是否一個帖子ID是在WordPress

if ('publish' == get_post_status ($post_id)) { 
    // do something 
} 

但我怎麼能檢查ID就像是在PHP不同:

if ($id != $var) { 
    //do stuff 
} 

我在做什麼:

$post_id = get_the_ID(); 
if ($post_id != 835) { 
    //do stuff 
} 

它是正確的嗎?

+0

你是什麼意思,如果id不同?不同於什麼? – Howli

+0

如果帖子ID與值不一樣(如1,10,50等):如果帖子ID不等於50 –

回答

1

然後使用以下任何一種方法都適用於您。如果當前帖子編號爲不是 8,則打印出「不投遞8」。如果你想要打印其他東西,你可以添加一個else語句。

<?php 
    $post_id = get_the_ID(); 
    if ($post_id != "8") { 
     echo "Not post 8"; 
    } 
?> 

<?php 
    $post_id = get_the_ID(); 
    if ($post_id != 8) { 
     echo "Not post 8"; 
    } 
?> 
相關問題