wordpress主题制作教程(十一):评论模板comments.php

前面我们制作了文章单页模板,我们可以发现单页模板的代码跟index.php差不多,不过今天我们让它们的差别大一点,我们给文章模板加入评论表单,让访客可以发表评论。

首先在主题文件夹下面新建一个文件comments.php,然后打开single.php文件,将里面的评论代码剪切出来,粘贴到comments.php文件中,要剪切的代码如下:

  1. <!– Comment’s List –>
  2.    <h3>Comments</h3>
  3.    <div class="hr dotted clearfix">&nbsp;</div>
  4.    <ol class="commentlist">
  5.        <li class="comment">
  6.            <div class="gravatar"> <img alt="" src=’images/gravatar.png’ height=’48′ width=’48′ /> <a class="comment-reply-link" href=&quot;>Reply</a> </div>
  7.            <div class="comment_content">
  8.                <div class="clearfix"> <cite class="author_name"><a href="">Joe Bloggs</a></cite>
  9.                    <div class="comment-meta commentmetadata">January 6, 2010 at 6:26 am</div>
  10.                </div>
  11.                <div class="comment_text">
  12.                    <p>Donec leo. Aliquam risus elit, luctus vel, interdum vitae, malesuada eget, elit. Nulla vitae ipsum. Donec ligula ante, bibendum sit amet, elementum quis, viverra eu, ante. Fusce tincidunt. Mauris pellentesque, arcu eget feugiat accumsan, ipsum mi molestie orci, ut pulvinar sapien lorem nec dui.</p>
  13.                </div>
  14.            </div>
  15.        </li>
  16.    </ol>
  17.    <div class="hr clearfix">&nbsp;</div>
  18.    <!– Comment Form –>
  19.    <form id="comment_form" action="" method="post">
  20.        <h3>Add a comment</h3>
  21.        <div class="hr dotted clearfix">&nbsp;</div>
  22.        <ul>
  23.            <li class="clearfix">
  24.                <label for="name">Your Name</label>
  25.                <input id="name" name="name" type="text" />
  26.            </li>
  27.            <li class="clearfix">
  28.                <label for="email">Your Email</label>
  29.                <input id="email" name="email" type="text" />
  30.            </li>
  31.            <li class="clearfix">
  32.                <label for="email">Your Website</label>
  33.                <input id="website" name="website" type="text" />
  34.            </li>
  35.            <li class="clearfix">
  36.                <label for="message">Comment</label>
  37.                <textarea id="message" name="message" rows="3" cols="40"></textarea>
  38.            </li>
  39.            <li class="clearfix">
  40.            <!– Add Comment Button –>
  41.            <a type="submit" class="button medium black right">Add comment</a> </li>
  42.        </ul>
  43.    </form>

然后在single.php文件原来的位置添加代码:

  1. <?php comments_template(); ?>

comments_template()函数默认的就是加载主题文件夹下面的comments.php文件,这个函数也是可以带参数的,以便让你可以加载别的文件,比如某些页面你需要加载一个不一样的评论表单,你就需要使用comments_template()带上参数,这里不细说。
为了防止某些恶意用户直接打开评论文件,我们在comments.php的头部添加代码:

  1. <?php
  2.     if (isset($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
  3.         die ('Please do not load this page directly. Thanks!');
  4. ?>

修改评论列表

wordpress有自动输出评论列表的函数wp_list_comments(),所以我们将原来的评论列表代码删除,换上这个函数,但是我们还需要加一些判断功能,比如评论需要密码才能查看、评论已经关闭、还没有评论这几个情况都要有不同的输出,所以将原来的评论代码:

  1. <li class="comment">
  2.     <div class="gravatar"> <img alt="" src=’images/gravatar.png’ height=’48′ width=’48′ /> <a class="comment-reply-link" href=&quot;>Reply</a> </div>
  3.     <div class="comment_content">
  4.         <div class="clearfix"> <cite class="author_name"><a href="">Joe Bloggs</a></cite>
  5.             <div class="comment-meta commentmetadata">January 6, 2010 at 6:26 am</div>
  6.         </div>
  7.         <div class="comment_text">
  8.         <p>Donec leo. Aliquam risus elit, luctus vel, interdum vitae, malesuada eget, elit. Nulla vitae ipsum. Donec ligul  a ante, bibendum sit amet, elementum quis, viverra eu, ante. Fusce tincidunt. Mauris pellentesque, arcu eget feugiat accumsan, ipsum mi molestie orci, ut pulvinar sapien lorem nec dui.</p>
  9.         </div>
  10.     </div>
  11. </li>

替换成:

  1. <?php
  2.    if (!empty($post->post_password) && $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) {
  3.        // if there's a password
  4.        // and it doesn't match the cookie
  5.    ?>
  6.    <li class="decmt-box">
  7.        <p><a href="#addcomment">请输入密码再查看评论内容.</a></p>
  8.    </li>
  9.    <?php
  10.        } else if ( !comments_open() ) {
  11.    ?>
  12.    <li class="decmt-box">
  13.        <p><a href="#addcomment">评论功能已经关闭!</a></p>
  14.    </li>
  15.    <?php
  16.        } else if ( !have_comments() ) {
  17.    ?>
  18.    <li class="decmt-box">
  19.        <p><a href="#addcomment">还没有任何评论,你来说两句吧</a></p>
  20.    </li>
  21.    <?php
  22.        } else {
  23.            wp_list_comments('type=comment&callback=aurelius_comment');
  24.        }
  25.    ?>

上面的wp_list_comments函数中我们家里两个参数,其中type=comment意思只输出评论,除了评论还有pings\trackback\ pingback等等什么的,callback=aurelius_comment意思是调用一个自定义的函数函数aurelius_comment来显示评论。
自定义的函数我们需要添加在主题的functions.php文件中,所以请在functions.php中的“?>”前面加上下面的代码,如果你的functions.php文件中已经存在了下面的代码,就不要再添加了:

  1. function aurelius_comment($comment$args$depth)
  2. {
  3.    $GLOBALS['comment'] = $comment; ?>
  4.    <li class="comment" id="li-comment-<?php comment_ID(); ?>">
  5.         <div class="gravatar"> <?php if (function_exists('get_avatar') && get_option('show_avatars')) { echo get_avatar($comment, 48); } ?>
  6.  <?php comment_reply_link(array_merge$argsarray('reply_text' => '回复','depth' => $depth, 'max_depth' => $args['max_depth']))) ?> </div>
  7.         <div class="comment_content" id="comment-<?php comment_ID(); ?>">
  8.             <div class="clearfix">
  9.                     <?php printf(__('<cite class="author_name">%s</cite>'), get_comment_author_link()); ?>
  10.                     <div class="comment-meta commentmetadata">发表于:<?php echo get_comment_time('Y-m-d H:i'); ?></div>
  11.                     &nbsp;&nbsp;&nbsp;<?php edit_comment_link('修改'); ?>
  12.             </div>
  13.             <div class="comment_text">
  14.                 <?php if ($comment->comment_approved == '0') : ?>
  15.                     <em>你的评论正在审核,稍后会显示出来!</em><br />
  16.         <?php endif; ?>
  17.         <?php comment_text(); ?>
  18.             </div>
  19.         </div>
  20.     </li>
  21. <?php } ?>

上面的自定义函数中用到的几个函数的说明如下:

  1. <?php
  2. get_avatar($id_or_email,$size,$default$alt);
  3. //$id_or_email这个参数必须,可以使一个用户ID、一个email,或者直接一个comment对象,上面代码就是直接将评论对象作为参数   
  4. //$size,这个参数就是头像大小,默认是96,上面代码设为32   
  5. //$default 一个图片地址,就是用户没有头像是显示的图片,默认是后台设置的那个   
  6. //$alt 就是图片的$alt信息了,我觉得alt信息应该用评论者名字  
  7. ?>
  1. <?php comment_reply_link();
  2. //回复链接
  3. ?>
  1. <?php
  2. get_comment_author_link();
  3. //获取评论者的链接
  4. ?>
  1. <?php
  2. get_comment_time();
  3. //获取评论时间
  4. edit_comment_link();
  5. //编辑评论的链接
  6. comment_text();
  7. //输出评论内容
  8. ?>

添加了上面的代码评论已经能正确显示了,接下来添加提交评论的表单。
评论表单
将原来comments.php中的评论表单代码删除:

  1. <!– Comment Form –>
  2. <form id="comment_form" action="" method="post">
  3.     <h3>Add a comment</h3>
  4.     <div class="hr dotted clearfix">&nbsp;</div>
  5.     <ul>
  6.         <li class="clearfix">
  7.             <label for="name">Your Name</label>
  8.             <input id="name" name="name" type="text" />
  9.         </li>
  10.         <li class="clearfix">
  11.             <label for="email">Your Email</label>
  12.             <input id="email" name="email" type="text" />
  13.         </li>
  14.         <li class="clearfix">
  15.             <label for="email">Your Website</label>
  16.             <input id="website" name="website" type="text" />
  17.         </li>
  18.         <li class="clearfix">
  19.             <label for="message">Comment</label>
  20.             <textarea id="message" name="message" rows="3" cols="40"></textarea>
  21.         </li>
  22.         <li class="clearfix">
  23.             <!– Add Comment Button –>
  24.             <a type="submit" class="button medium black right">Add comment</a> </li>
  25.     </ul>
  26. </form>

实际上你不需要再手动输入每个表单项了,新版的wordprss提供了一个非常方便的函数:comment_form(),添加代码如下:

  1. <?php if ( comments_open() ) : ?>
  2. <?php if ( get_option('comment_registration') && !$user_ID ) : ?>
  3. <p><?php printf(__('你需要先 <a href="%s">登录</a> 才能发表评论.'), get_option('siteurl')."/wp-login.php?redirect_to=".urlencode(get_permalink()));?></p>
  4. <?php else : ?>
  5. <?php $defaults = array(
  6.     'comment_notes_before' => '',
  7.     'label_submit'         => __( '提交评论' ),
  8.     'comment_notes_after' =>''
  9. );
  10. comment_form($defaults);
  11.  endif;
  12. else :  ?>
  13. <p><?php _e('对不起评论已经关闭.'); ?></p>
  14. <?php endif; ?>

可以看到上面的代码中也添加了判断,看是否允许评论,是否需要登录才能评论。
你完全可以通过comment_form()函数的各个参数再配合css输出一个个性化的表单,这在以后的教程中讲。

本篇教程之前的几篇教程是

本篇教程之后的几篇教程是

没有找到你要找的内容?你可以通过搜索你要找的内容,或者给我们留言。

已有34条评论

伟德国际1946进行回复 取消回复