又可以评论啦by ADMIN on 二月 27,2017

之前一直也没怎么打理博客,不知道从什么时候开始,就无法正确提交评论了,却全然不知,直到最近才发现。
主要问题表现为:提交评论的时候停留在wp-comments-post.php空白页面,换一个官方自带的主题后又没有问题,可以评论。
既然这样,那肯定不用说是主题的问题。除了评论其他一切正常,那肯定是评论模板又问题。
打开模板文件comments.php 找到评论功能全部代码为:


<?php if ('open' == $post->comment_status) : ?>
	<div id="commentform">
		<h3><?php _e('Write a comment'); ?>:</h3>
	<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
		<p><?php _e('You have to'); ?> <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php the_permalink(); ?>"><?php _e('log in'); ?></a> <?php _e('to write a comment.'); ?></p>
	<?php else : ?>
  <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" name="comment-form">
		<?php if ( $user_ID ) : ?>
			<p><a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a> | <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="<?php _e('Log out of this account') ?>"><?php _e('Logout') ?> »</a></p>
		<?php else : ?>
			<p>
				<input type="text" name="author" id="author" class="input-text" value="<?php echo $comment_author; ?>" size="22" />
				<label for="author"><?php _e('Name'); ?> <?php if ($req) _e('(needed)'); ?></label>
			</p>
			<p>
				<input type="text" name="email" id="email" class="input-text" value="<?php echo $comment_author_email; ?>" size="22" />
				<label for="email"><?php _e('Email'); ?> <?php if ($req) _e('(needed but it will not be published)'); ?></label>
			</p>
			<p>
				<input type="text" name="url" id="url" class="input-text" value="<?php echo $comment_author_url; ?>" size="22" />
				<label for="url"><?php _e('Website (if you have one)'); ?></label>
			</p>
		<?php endif; ?>
			<p>
				<textarea name="comment" id="comment" cols="100%" rows="10"></textarea>
			</p>
			<p class="input-submit">
				<input name="submit-comment" type="submit" id="submit-comment" value="<?php _e('确认'); ?>" />
				<input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />
			</p>
		<?php do_action('comment_form', $post->ID);  ?>
		</form>
  </div><!-- close:commentform -->
	<?php endif; // If registration required and not logged in ?>
<?php endif; // if you delete this the sky will fall on your head ?>

其中有一句
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" name="comment-form">
问题就是这句话引起的,这句是调用了wp-comments-post.php文件来实现评论功能,从某个版本后,就不能使用这种方法了来实现了,而是用函数 comment_form() 来实现评论,而且这个函数包含了各个表单。
将原先的全部评论代码删除,添加如下代码:


<?php if ( comments_open() ) : ?>
	<div id="commentform">
		<h3><?php _e('Write a message'); ?>:</h3>	
		<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
		<p><?php _e('You have to'); ?> <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php the_permalink(); ?>"><?php _e('log in'); ?></a> <?php _e('to write a message.'); ?></p>
	<?php else : ?>
		
    <?php   
    add_filter('comment_form_default_fields','my_custom_fields');   
    function my_custom_fields($fields){   
    
    $fields =  array(   
            'author' => '<p class="comment-form-author"><input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" ' . $aria_req . ' />
			<label for="author">' . __( 'Nickname' ) . '</label> ' . ( $req ? '' : '<span class="required">(needed)</span>' ) .  '</p>',
            'email'  => '<p class="comment-form-email"><input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" ' . $aria_req . ' />
			<label for="email">' . __( 'E-mail' ) . '</label> ' . ( $req ? '' : '<span class="required">(needed but it will not be published)</span>' ) .  '</p>',   
            'url'    => '<p class="comment-form-url"><input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" />
			<label for="url">' . __( 'Website (if you have one)' ) . '</label></p>',   
        );  
    
        return $fields;   
    }   
    ?>  
      
    	<?php $defaults = array( 
			
			'comment_field'        => '<p class="comment-form-comment"><textarea id="comment" name="comment" aria-required="true"></textarea></p>', 
        		'comment_notes_before' => '',   
        		'label_submit'         => __( '提交评论' ),   
        		'comment_notes_after' =>'',
    				);   
    			comment_form($defaults);    
  		?>  
	</div><!-- close:commentform -->
	<?php endif; // If registration required and not logged in ?>
	<?php endif; // if you delete this the sky will fall on your head ?>

There are 2 comments in this article:

    1. ADMIN说道:

      @FROYO  过奖,过奖,我也是在网上摸索的