首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress have_comments不工作

Wordpress have_comments不工作
EN

Stack Overflow用户
提问于 2013-08-20 07:56:37
回答 3查看 5.8K关注 0票数 2

我在wordpress中设置了注释()条件。这个条件是在comments.php中设置的,就像wordpress默认主题一样。

然后使用comments.php加载整个comment_template文件;现在,当我删除have_comments()条件时,一切正常,所有的注释都被加载,但是当我添加这个条件时,它返回false,就好像没有注释一样。

以下是我的整个comments.php文件:

代码语言:javascript
复制
<?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   
    }
    ?>

提前感谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-20 08:04:36

你先打电话给have_comments(),然后再打给get_comments()

这可能只是问题,你有错误的处理-流程在这里。Wordpress利用全局静态,所以事情的顺序很重要(而且很容易被忽略):

代码语言:javascript
复制
<?php

    $comments = get_comments();

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

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

此外,法典说,have_comments()依赖于循环,所以这就是$post。可能就连我上面的示例代码建议也没有让它正确地处理静态状态,所以您需要麻烦

例如,当get_comments()返回一个数组时,通常会这样做:

代码语言:javascript
复制
<?php

    $comments = get_comments();

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

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

正如您所看到的,对have_comments()的调用是不必要的。

希望这能帮上忙,保重。

票数 7
EN

Stack Overflow用户

发布于 2017-01-30 09:45:07

在设置注释之前查询注释时会出现此问题。您可以使用comments_template(..)加载包含have_comments()的页面,以正确设置注释。

票数 4
EN

Stack Overflow用户

发布于 2021-11-25 15:41:29

为此我花了几个小时。可能会对某些人有帮助。

当我试图在我的模板中使用get_template_part('comments');在页面上使用comments.php时,它不起作用,have_comments();还给了我bool(false)

谢谢你这篇文章。我使用了comments_template();而不是get_template_part。而且它是有效的。我认为主要原因是Wordpress使用了全局静态,您需要使用comments_template()和单个post或页面来保存它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18329755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档