2016-10-15 171 views
-2

我遇到了多個if語句的問題。我正在使用&&,但它似乎只適用於第一個聲明。 的代碼是像這樣的:多個if語句問題

global $post; 
$args = array('post_id' => $post->ID); 
$comment = get_comments($args); 
$user = wp_get_current_user(); 
if (3 <= count($comment) && $post->post_author == $user->ID) { 
    echo do_shortcode('[button]'); 
} else { 
    comment_form(); 
} 

它主要統計,如果有小於3分的評論則顯示評論的形式,但如果有3個以上,是文章作者然後顯示一個按鈕。該按鈕僅在有超過3條評論的情況下才會顯示。它不會檢查它是否僅僅是帖子作者,就像我想要的那樣。

+2

你試過打印'計數($評論)','$後> post_author'和'$用戶> ID',以確保他們的價值是你所需要的值? – CristianHG

回答

0

你所描述的是兩種情況下應該顯示按鈕。所以必須有兩種方法才能進入if塊。你必須用邏輯或運算符||來重構你的if語句。

例如:

if($post->post_author == $user->ID || 3 <= count($comment)) { 
    echo do_shortcode('[button]'); 
} else { 
    comment_form(); 
} 
+0

是的,我試過測試這些值,它們是正確的。 – David

+0

糟糕,它在我完成之前發送。值是正確的,我已經嘗試使用ll而不是&&,但沒有改變。我也試圖做出獨立的條件,但是當我這樣做時,評論表格仍然會顯示它是否已經收到3這樣做。 – David

+0

當用戶 - > ID不是post_author或count($ comment)> 3時,將執行comment_form()。如果這不是你想要的,請再次告訴,何時應該執行comment_form()或何時不執行。 OP認爲我有點誤導。 –

0

我不得不改變像這樣的代碼來得到它的工作。

global $post,$current_user; 
 
$args = array('post_id' => $post->ID); 
 
$comment = get_comments($args); 
 
    get_currentuserinfo(); 
 
    if ($post->post_author == $current_user->ID) { 
 
    echo do_shortcode('[button]'); 
 
} elseif (3 <= count($comment)) { 
 
// blank 
 
} else { 
 
    comment_form(); 
 
}