2013-08-20 48 views
1

我已經在wordpress中設置了評論()條件。這個條件在comments.php中設置,就像WordPress的默認主題一樣。WordPress的have_comments不起作用

然後整個comments.php文件使用comment_template加載;現在,當我刪除have_comments()條件時,一切正常,所有的註釋都被加載,但是當我添加這個條件時,它會返回false,就好像沒有註釋。

這是我整個的comments.php文件:

<?php 
/** 
| This page deals with the comment-system and template in the Behdis Marketing Group Wordpress Theme. 
**/ 
$commenter = wp_get_current_commenter(); 
$req = get_option('require_name_email'); 
$aria_req = ($req ? " aria-required='true'" : ''); 
$fields = array(
    'author' => "<div><input type='text' name='author' placeholder='Full Name' /></div>", 
    'email' => "<div><input type='text' name='email' placeholder='Email /></div>", 
); 

$comments_args = array(
    'fields' => $fields, 
    'comment_field' => "<div class=\"comment-component\"><textarea name=\"comment\" id=\"comment\" ></textarea></div>", 
    'comment_notes_after' => '', 
    'title_reply' => 'Write your comment...', 
    'title_reply_to' => 'Reply', 
    'label_submit' => 'Comment!', 
    'comment_notes_before' => "<p class='simple-title'>" . 'Your email is kept secret forever' . ' ' 
); 

comment_form($comments_args); 
?> 
<?php 

    if(have_comments()) 
    {  
?> 
<section class='post-comments'> 

    <?php 
     $comments = get_comments(); 
     foreach($comments as $comm) 
     { 
      ?> 
      <div class='post-each-comment'> 
      <p class="post-each-comment-meta"> 
      <?php echo $comm->comment_author;?> در تاریخ <?php comment_time();?> 
      </p> 
      <?php echo $comm->comment_content; ?> 
      </div> 
      <?php 
     } 
     ?> 
</section> 
    <?php 
    }// end of have_comments() 
    else 
    { 
     ?> 
     <div class='no-comment' > 
      No comments, be the first visitor to comment on this post! 
     </div> 
     <?php 
    } 
    ?> 

在此先感謝

回答

3

你叫have_comments()你叫get_comments()之前。

這可能只是你在這裏有錯誤的處理流程的問題。 WordPress的利用全球靜態的這樣的東西的順序是重要的(容易錯過):

<?php 

    $comments = get_comments(); 

    if(have_comments()) 
    {  
?> 
<section class='post-comments'> 

    <?php   
     foreach($comments as $comm) 
     { 
      ?> 
      <div class='post-each-comment'> 

而且CODEX說have_comments()取決於環路所以這是$post。這可能是,即使我上面的示例代碼建議並沒有使它適當地處理靜態,所以你需要麻煩 - 拍攝這是有點找出用什麼。

E.g.作爲get_comments()返回數組這通常做的:

<?php 

    $comments = get_comments(); 

    if($comments) 
    {  
?> 
<section class='post-comments'> 

    <?php   
     foreach($comments as $comm) 
     { 
      ?> 
      <div class='post-each-comment'> 

正如你可以看到have_comments()通話是沒有必要的。

希望這可以幫助和照顧。

+0

抱歉,您的意思是我必須在get_comments()之後放置have_comments()?我已經在上面的代碼之前測試過了,它們都不起作用。 –

+0

@Mostafa:正如所寫的,可能不會這樣做,請參閱第二個例子,它根本不使用'has_comments()'。 AFAIK'has_comments()'用於循環頁面而不是評論頁面。 – hakre

+0

有人幫助我!我被這個問題阻止了 –

0

當您在設置註釋之前查詢註釋時會發生此問題。您可以使用comments_template(..)加載包含have_comments()的頁面以正確設置註釋。