【问题标题】:Comment form won't show on my wp blog posts评论表不会显示在我的 wp 博客文章中
【发布时间】:2015-05-31 02:08:11
【问题描述】:

我有一个带有自定义主题的 wordpress 网站,并且我已将 cmets 包含在我的 single.php 文件中,如下所示:

    <?php get_header();?>
<div id="content">
<div class="right">
<div class="article">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<small><?php the_time('F jS, Y'); ?></small>
<?php the_content();?>
<br /><br />
<hr />
<br /><br />
<?php comments_template(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>

<?php endif; ?>
</div></div>
<?php get_sidebar(); ?>
</div>
<?php get_footer();?>

不过,评论表单不会显示在我的博文下方。我在后端启用了 cmets。

【问题讨论】:

  • 发布您用于此表单的插件名称。
  • 我正在使用(至少尝试使用)wordpress 附带的内置评论系统。至于插件:我尝试了几个评论插件,但都没有工作,所以肯定有问题,但网站上的其他一切都很好。

标签: php wordpress comments


【解决方案1】:

我不知道您的模板代码是否正确,但您的代码包含错误,因为您使用错误位置放置注释模板代码

将注释模板代码放在循环之外和container 类中 像这样

<?php get_header();?>
<div id="content">
<div class="right">
<div class="article">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<small><?php the_time('F jS, Y'); ?></small>
<?php the_content();?>
<br /><br />
<hr />
<br /><br />

<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>

<?php endif; ?>
</div>
<?php comments_template(); ?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer();?>

我的代码 comments.php 文件中的这段代码

<?php
/* Prevent the direct loading of comments.php */
if (!empty($_SERVER['SCRIPT-FILENAME']) && basename($_SERVER['SCRIPT- FILENAME']) == 'comments.php')  {
die(__('You can not access this file directly.', 'Yallanpe-Theme'));
}
/* If the post is password protected then display text and return */
if(post_password_required()) : ?>
<h5>
<?php 
    _e('This Post is password protected, Enter the password to view the  comments', 'Yallanpe-Theme'); 
    return; 
?>
</h5>
<?php endif;
/* If we have comments to display, we display them */
if(have_comments()) : ?>
<div id="comments-container">
    <?php wp_list_comments( array('walker' => new YPE_walker_comment)); ?>
    <script>
    $(document).ready(function() {
        $("#comments-container ul#comment-list li.depth-1 .comment-avatar  img").addClass("img-thumbnail");
            $("#comments-container ul#comment-list li.depth-1 ul.children  .comment-avatar img").addClass("img-circle");
        });
    </script>
</div><!-- /#comments-container -->
<?php if (get_comment_pages_count() > 1 && get_option('page_comments')) : ?>
    <div class="comments-nav-section">
        <h5 class="text-left"><?php previous_comments_link(__('&larr; Older Comments', 'Yallanpe-Theme')); ?></h5>
        <h5 class="text-right"><?php next_comments_link(__('Newer Comments &rarr;', 'Yallanpe-Theme')); ?></h5>
    </div><!-- end comment-nav-section -->
<?php endif; ?>
<?php 
/* If we don't have comments and the comments are closed, display a text */
elseif (!comments_open() && !is_page() &&  post_type_supports(get_post_type(), 'comments')) : ?>
<h5> <?php _e('Comments are Closed.', 'Yallanpe-Theme'); ?> </h5>
<?php endif; 
/* Display the comment form */
comment_form();
?>

下面的代码在functions.php 中(我使用Bootstarp 框架作为评论表单)

<?php

class YPE_walker_comment extends Walker_Comment {

var $tree_type = 'comment';
var $db_fields = array( 'parent' => 'comment_parent', 'id' => 'comment_ID'    );

function __construct() { ?>
    <h4>Comments</h4>
    <ul id="comment-list">
<?php }
function start_lvl( &$output, $depth = 0, $args = array() ) {       
    $GLOBALS['comment_depth'] = $depth + 1; ?>
    <ul class="children">
<?php }
function end_lvl( &$output, $depth = 0, $args = array()) {
    $GLOBALS['comment_depth'] = $depth + 1; ?>
    </ul>
<?php }
function start_el( &$output, $comment, $depth, $args, $id = 0 ) {
    $depth++;
    $GLOBALS['comment_depth'] = $depth;
    $GLOBALS['comment'] = $comment; 
    $parent_class = (empty($args['has_children']) ? '' : 'parent'); ?>

    <li <?php comment_class($parent_class); ?> id="comment-<?php  comment_ID() ?>">

        <div class="row">
            <div class="col-sm-2">
                <div class="comment-avatar">
                    <?php
                        $avatar_size = 80;
                        if ($comment->comment_parent != 0) { $avatar_size =  80; }
                        echo get_avatar($comment, $avatar_size);
                    ?>
                </div>
            </div>

            <div class="col-sm-10">
                 <div class="comment-header">
                    <?php
                        $reply_args = array(
                            'add_below' => $add_below, 
                            'depth' => $depth,
                            'max_depth' => $args['max_depth'] 
                        );
                    ?>
                        <ul id='author-time' class="list-inline">
                            <li><?php echo get_comment_author_link(); ?> </li>
                            <li class="pull-right text-muted"><?php  comment_date(); ?> at <?php comment_time(); ?></li>
                        </ul>
                        <em>
                            <ul id="reply-delete" class="list-inline">
                                <li><?php  comment_reply_link(array_merge($args, $reply_args)); ?></li>
                                <?php if (current_user_can('edit_post')) {   $id = get_comment_ID(); ?>
                                    <li><?php echo ' <a   href="'.admin_url("comment.php?action=editcomment&c=$id").'">Edit</a>'; ?></li>
                                    <li><?php echo ' <a href="'.admin_url("comment.php?action=cdc&c=$id").'">Delete</a>'; ?></li>
                                    <li><?php echo ' <a href="'.admin_url("comment.php?action=cdc&dt=spam&c=$id").'">Spam</a>'; ?></li>
                                <?php } ?>
                            </ul>
                        </em>
                </div>

                <div class="comment-content">
                    <?php 
                        if( !$comment->comment_approved ) : ?>
                        <em class="text-muted">
                            <?php _e('Your comment is awaiting moderation.',  'Yallanpe Theme'); ?>
                        </em>
                    <?php else: comment_text(); ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
<?php }
    function end_el( &$output, $comment, $depth = 0, $args = array() ) { ?>
    </li> 
<?php }
    function __destruct() { ?>
    </ul>
<?php }
}

function YPE_custom_comment_form($defaults) {
$comment_notes_after = '';
$defaults ['comment_notice_before'] = '';
$defaults ['comment_notes_after'] = $comment_notes_after;
$defaults ['id_form'] = 'commentform';
$defaults ['comment_field'] = '<p class="form-group">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <span class="input-group-addon"> <span class="glyphicon glyphicon-comment"></span></span>
                                                <textarea class="form- control" name="comment" id="comment" cols="30" rows="10">
                                                </textarea>
                                        </div>
                                    </div>
                                </p>';
$defaults ['label_submit']  = 'Submit Comment';
$defaults ['title_reply']   = '<section id="comment-form"><h4>Add  Comment</h4></section>';

return $defaults;
}

add_filter ('comment_form_defaults', 'YPE_custom_comment_form');


function YPE_custom_comment_fields() {
$commenter = wp_get_current_commenter();
$req       = get_option('require_name_email');
$aria_req  = ($req ? " aria-required='true'" : '');

$fields = array(
    'author' => '<div class="form-group">' .
                    '<p>'.
                        '<div class="form-group">
                            <div class="input-group">
                                <span class="input-group-addon"><span  class="glyphicon glyphicon-user"></span></span>' .
                                    '<input type="text" class="form-control"  id="author" name="author" value="'. esc_attr($commenter['comment_author']) .'"  '. $aria_req .' placeholder="Enter Your Name" />'.
                            '</div>'.
                        '</div>'.
                    '</p>'.
                '</div>',
    'email' => '<div class="form-group">' .
                    '<p>'.
                        '<div class="form-group">
                            <div class="input-group">
                                <span class="input-group-addon"><span  class="glyphicon glyphicon-envelope"></span></span>' .
                                    '<input type="text" class="form-control"  id="email" name="email" value="'. esc_attr($commenter['comment_author_email'])  .'" '. $aria_req .' placeholder="Enter Your Email" />'.
                            '</div>'.
                        '</div>'.
                    '</p>'.
                '</div>',
    'url' => '<div class="form-group">' .
                    '<p>'.
                        '<div class="form-group">
                            <div class="input-group">
                                <span class="input-group-addon"><span  class="glyphicon glyphicon-globe"></span></span>' .
                                    '<input type="text" class="form-control"  id="url" name="url" value="'.esc_attr($commenter['comment_author_url']) .'"  placeholder="Enter Your Website URL" />'.
                            '</div>'.
                        '</div>'.
                    '</p>'.
                '</div>',

);
return $fields;
}

add_filter ('comment_form_default_fields', 'YPE_custom_comment_fields');


?>

【讨论】:

  • 感谢编辑!但我仍然无法在我的博文下方看到评论表单。
  • 一定是你在评论模板代码中有错误,因为上面的代码是正确的,如果你的模板代码不起作用,那么循环外的评论模板必须是正确的我在主题中使用的完整代码
  • comments_template(); 接受两个参数(可选),但如果您对注释代码使用不同的文件名,例如my-comment.php,您必须指向第一个参数中的文件名,如comments_template( '/my-comment.php' );评论文件的默认名称是comments.php
  • 是的,我为我的 cmets.php 文件尝试了几种解决方案,还从第二十三模板中复制了 cmets.php。不工作。我应该对第二十三个模板进行一些编辑吗?
  • 您是否在管理面板中激活了 cmets?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
  • 2014-02-26
  • 2016-02-02
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
相关资源
最近更新 更多