2015-05-29 52 views
0

我有一個WordPress站點使用自定義的主題,我已作了評論到我的single.php文件,它看起來像這樣:評論的形式將不顯示在我的wp博客文章

<?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();?> 

儘管如此,評論表單不會顯示在我的博客文章下方。我已在後端啓用了評論。

+0

張貼您正在使用這種形式的插件的名稱。 –

+0

我正在使用(至少試圖)與WordPress一起內置的評論系統。至於插件:我嘗試了幾個評論插件,並沒有一個工作,所以肯定有什麼問題,但網站上的其他一切正常。 –

回答

0

我不知道你的模板代碼是正確的或沒有,但你的代碼包含錯誤,因爲你用錯誤的位置把註釋模板代碼

把註釋模板代碼外循環和 的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'); 


?> 
+0

感謝您的編輯!但是我仍然無法在我的博客文章中看到評論表單。 –

+0

必須是你有評論模板代碼中的錯誤,因爲上面的代碼是正確的,必須在循環以外的評論模板,如果不工作你的模板代碼它包含錯誤,如果你螞蟻我帶你我完整的代碼在我的主題 –

+0

'comments_template();'它接受兩個參數(可選),但如果您使用不同的文件名作爲評論代碼,例如我們的'my-comment.php',您必須指向第一個參數中的文件名,例如'comments_template '/my-comment.php');'評論文件的默認名稱是'comments.php' –