原文:The WordPress Theme Comments Template
原文作者:Ian Stewart
译文作者:Young
是的,我讨厌评论模板,因为它可能相当令人困惑。在 2.7 版本上,WordPress 介绍了一个简单的创建评论模板的方法,但是那种方法不能使留言和引用分开,也不能自定义评论结构,并且仍然令人困惑。
您很幸运,我把问题解决了,虽然还是有点令人困惑,但毕竟解决了问题。在这个评论模板的教程上,我将基本上介绍每一步会发生什么,展示您那些自定义代码,以及需要添加到 function.php 的代码,最后给出整个模板代码。希望这对您有帮助。但是至少您将会有一个漂亮的评论模板。
让我们简要的看一下这个评论模板会发生什么事:
- 阻止机器人抓取评论内容以及受密码保护的文章;
- 检查是否有评论;
- 计算留言和引用 (trackbacks or pings) 的数量;
- 如果有评论,显示评论,并链接到评论;
- 如果有引用,显示引用;
- 如果评论被启用,显示评论“回复”框。
这个模板的代码很多,但是总结起来也就这几个功能,直截了当。
自定义回调留言和引用
WordPress 2.7 允许通过 wp_list_comments() 函数方便有序地调用单独日志的留言和引用。如果您喜欢那样,那就很方便。但是我们不喜欢,我想要把评论里面的留言和引用分开。
为了实现这种结果,您需要对留言和引用进行自定义回调,把下面代码插入到 functions.php 文件:
- // Custom callback to list comments in the your-theme style
- function custom_comments($comment, $args, $depth) {
- $GLOBALS['comment'] = $comment;
- $GLOBALS['comment_depth'] = $depth;
- ?>
- <li id="comment-<?php comment_ID() ?>" <?php comment_class() ?>>
- <div class="comment-author vcard"><?php commenter_link() ?></div>
- <div class="comment-meta"><?php printf(__(‘Posted %1$s at %2$s <span class="meta-sep">|</span> Permalink’, ‘your-theme’),
- get_comment_date(),
- get_comment_time(),
- ‘#comment-’ . get_comment_ID() );
- edit_comment_link(__(‘Edit’, ‘your-theme’), ‘ <span class="meta-sep">|</span> <span class="edit-link">’, ‘</span>’); ?></div>
- <?php if ($comment->comment_approved == ’0′) _e("\t\t\t\t\t<span class=’unapproved’>Your comment is awaiting moderation.</span>\n", ‘your-theme’) ?>
- <div class="comment-content">
- <?php comment_text() ?>
- </div>
- <?php // echo the comment reply link
- if($args['type'] == ‘all’ || get_comment_type() == ‘comment’) :
- comment_reply_link(array_merge($args, array(
- ‘reply_text’ => __(‘Reply’,'your-theme’),
- ‘login_text’ => __(‘Log in to reply.’,'your-theme’),
- ‘depth’ => $depth,
- ‘before’ => ‘<div class="comment-reply-link">’,
- ‘after’ => ‘</div>’
- )));
- endif;
- ?>
- <?php } // end custom_comments
- // Custom callback to list pings
- function custom_pings($comment, $args, $depth) {
- $GLOBALS['comment'] = $comment;
- ?>
- <li id="comment-<?php comment_ID() ?>" <?php comment_class() ?>>
- <div class="comment-author"><?php printf(__(‘By %1$s on %2$s at %3$s’, ‘your-theme’),
- get_comment_author_link(),
- get_comment_date(),
- get_comment_time() );
- edit_comment_link(__(‘Edit’, ‘your-theme’), ‘ <span class="meta-sep">|</span> <span class="edit-link">’, ‘</span>’); ?></div>
- <?php if ($comment->comment_approved == ’0′) _e(‘\t\t\t\t\t<span class="unapproved">Your trackback is awaiting moderation.</span>\n’, ‘your-theme’) ?>
- <div class="comment-content">
- <?php comment_text() ?>
- </div>
- <?php } // end custom_pings
这些代码看起来有点乱,是吧?但它们是最好的。现在到创建评论结构了,我想下面那个结构很漂亮,并且能够让您只通过 CSS 就能更改很多 —— 如果您真的想修改这个结构。
我们还需要调用一个特殊的自定义函数,这个函数将会显示符合 microformat hcard 架构的头像结构。
- // Produces an avatar image with the hCard-compliant photo class
- function commenter_link() {
- $commenter = get_comment_author_link();
- if ( ereg( ‘<a[^>]* class=[^>]+>’, $commenter ) ) {
- $commenter = ereg_replace( ‘(<a[^>]* class=[\'"]?)’, ‘\\1url ‘ , $commenter );
- } else {
- $commenter = ereg_replace( ‘(<a )/’, ‘\\1class="url "’ , $commenter );
- }
- $avatar_email = get_comment_author_email();
- $avatar = str_replace( "class=’avatar", "class=’photo avatar", get_avatar( $avatar_email, 80 ) );
- echo $avatar . ‘ <span class="fn n">’ . $commenter . ‘</span>’;
- } // end commenter_link
(以上代码插入于 functions.php 文件中。——译者注)
如果想改变头像的尺寸,您只需要改一下 get_avatar($avatar_email, 80) 里面的 80 就可以了,80 表示头像的大小,单位是像素。
评论模板
没有把您吓跑吧?诚然,并不是很可怕。下面是整个评论模板代码,上面有些 PHP 注释,以便您的理解。
- <?php /* The Comments Template — with, er, comments! */ ?>
- <div id="comments">
- <?php /* Run some checks for bots and password protected posts */ ?>
- <?php
- $req = get_option(‘require_name_email’); // Checks if fields are required.
- if ( ‘comments.php’ == basename($_SERVER['SCRIPT_FILENAME']) )
- die ( ‘Please do not load this page directly. Thanks!’ );
- if ( ! empty($post->post_password) ) :
- if ( $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password ) :
- ?>
- <div class="nopassword"><?php _e(‘This post is password protected. Enter the password to view any comments.’, ‘your-theme’) ?></div>
- </div><!– .comments –>
- <?php
- return;
- endif;
- endif;
- ?>
- <?php /* See IF there are comments and do the comments stuff! */ ?>
- <?php if ( have_comments() ) : ?>
- <?php /* Count the number of comments and trackbacks (or pings) */
- $ping_count = $comment_count = 0;
- foreach ( $comments as $comment )
- get_comment_type() == "comment" ? ++$comment_count : ++$ping_count;
- ?>
- <?php /* IF there are comments, show the comments */ ?>
- <?php if ( ! empty($comments_by_type['comment']) ) : ?>
- <div id="comments-list" class="comments">
- <h3><?php printf($comment_count > 1 ? __(‘<span>%d</span> Comments’, ‘your-theme’) : __(‘<span>One</span> Comment’, ‘your-theme’), $comment_count) ?></h3>
- <?php /* If there are enough comments, build the comment navigation */ ?>
- <?php $total_pages = get_comment_pages_count(); if ( $total_pages > 1 ) : ?>
- <div id="comments-nav-above" class="comments-navigation">
- <div class="paginated-comments-links"><?php paginate_comments_links(); ?></div>
- </div><!– #comments-nav-above –>
- <?php endif; ?>
- <?php /* An ordered list of our custom comments callback, custom_comments(), in functions.php */ ?>
- <ol>
- <?php wp_list_comments(‘type=comment&callback=custom_comments’); ?>
- </ol>
- <?php /* If there are enough comments, build the comment navigation */ ?>
- <?php $total_pages = get_comment_pages_count(); if ( $total_pages > 1 ) : ?>
- <div id="comments-nav-below" class="comments-navigation">
- <div class="paginated-comments-links"><?php paginate_comments_links(); ?></div>
- </div><!– #comments-nav-below –>
- <?php endif; ?>
- </div><!– #comments-list .comments –>
- <?php endif; /* if ( $comment_count ) */ ?>
- <?php /* If there are trackbacks(pings), show the trackbacks */ ?>
- <?php if ( ! empty($comments_by_type['pings']) ) : ?>
- <div id="trackbacks-list" class="comments">
- <h3><?php printf($ping_count > 1 ? __(‘<span>%d</span> Trackbacks’, ‘your-theme’) : __(‘<span>One</span> Trackback’, ‘your-theme’), $ping_count) ?></h3>
- <?php /* An ordered list of our custom trackbacks callback, custom_pings(), in functions.php */ ?>
- <ol>
- <?php wp_list_comments(‘type=pings&callback=custom_pings’); ?>
- </ol>
- </div><!– #trackbacks-list .comments –>
- <?php endif /* if ( $ping_count ) */ ?>
- <?php endif /* if ( $comments ) */ ?>
- <?php /* If comments are open, build the respond form */ ?>
- <?php if ( ‘open’ == $post->comment_status ) : ?>
- <div id="respond">
- <h3><?php comment_form_title( __(‘Post a Comment’, ‘your-theme’), __(‘Post a Reply to %s’, ‘your-theme’) ); ?></h3>
- <div id="cancel-comment-reply"><?php cancel_comment_reply_link() ?></div>
- <?php if ( get_option(‘comment_registration’) && !$user_ID ) : ?>
- <p id="login-req"><?php printf(__(‘You must be logged in to post a comment.’, ‘your-theme’),
- get_option(‘siteurl’) . ‘/wp-login.php?redirect_to=’ . get_permalink() ) ?></p>
- <?php else : ?>
- <div class="formcontainer">
- <form id="commentform" action="<?php echo get_option(‘siteurl’); ?>/wp-comments-post.php" method="post">
- <?php if ( $user_ID ) : ?>
- <p id="login"><?php printf(__(‘<span class="loggedin">Logged in as %2$s.</span> <span class="logout">Log out?</span>’, ‘your-theme’),
- get_option(‘siteurl’) . ‘/wp-admin/profile.php’,
- wp_specialchars($user_identity, true),
- wp_logout_url(get_permalink()) ) ?></p>
- <?php else : ?>
- <p id="comment-notes"><?php _e(‘Your email is <em>never</em> published nor shared.’, ‘your-theme’) ?> <?php if ($req) _e(‘Required fields are marked <span class="required">*</span>’, ‘your-theme’) ?></p>
- <div id="form-section-author" class="form-section">
- <div class="form-label"><label for="author"><?php _e(‘Name’, ‘your-theme’) ?></label> <?php if ($req) _e(‘<span class="required">*</span>’, ‘your-theme’) ?></div>
- <div class="form-input"><input id="author" name="author" type="text" value="<?php echo $comment_author ?>" size="30" maxlength="20" tabindex="3" /></div>
- </div><!– #form-section-author .form-section –>
- <div id="form-section-email" class="form-section">
- <div class="form-label"><label for="email"><?php _e(‘Email’, ‘your-theme’) ?></label> <?php if ($req) _e(‘<span class="required">*</span>’, ‘your-theme’) ?></div>
- <div class="form-input"><input id="email" name="email" type="text" value="<?php echo $comment_author_email ?>" size="30" maxlength="50" tabindex="4" /></div>
- </div><!– #form-section-email .form-section –>
- <div id="form-section-url" class="form-section">
- <div class="form-label"><label for="url"><?php _e(‘Website’, ‘your-theme’) ?></label></div>
- <div class="form-input"><input id="url" name="url" type="text" value="<?php echo $comment_author_url ?>" size="30" maxlength="50" tabindex="5" /></div>
- </div><!– #form-section-url .form-section –>
- <?php endif /* if ( $user_ID ) */ ?>
- <div id="form-section-comment" class="form-section">
- <div class="form-label"><label for="comment"><?php _e(‘Comment’, ‘your-theme’) ?></label></div>
- <div class="form-textarea"><textarea id="comment" name="comment" cols="45" rows="8" tabindex="6"></textarea></div>
- </div><!– #form-section-comment .form-section –>
- <div id="form-allowed-tags" class="form-section">
- <p><span><?php _e(‘You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes:’, ‘your-theme’) ?></span> <code><?php echo allowed_tags(); ?></code></p>
- </div>
- <?php do_action(‘comment_form’, $post->ID); ?>
- <div class="form-submit"><input id="submit" name="submit" type="submit" value="<?php _e(‘Post Comment’, ‘your-theme’) ?>" tabindex="7" /><input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" /></div>
- <?php comment_id_fields(); ?>
- <?php /* Just … end everything. We’re done here. Close it up. */ ?>
- </form><!– #commentform –>
- </div><!– .formcontainer –>
- <?php endif /* if ( get_option(‘comment_registration’) && !$user_ID ) */ ?>
- </div><!– #respond –>
- <?php endif /* if ( ‘open’ == $post->comment_status ) */ ?>
- </div><!– #comments –>
就这么多了,您已经创建了一个自定义的漂亮的评论模板。
如何创建 WordPress 主题
这篇文章是如何创建 WordPress 主题教程系列的其中一部分,该系列将会教您如何从零开始创建强大的 WordPress 主题。建议您从头开始阅读这个系列并自己动手编写一些漂亮的代码。
相关博文:

真的是有点恐惧主题中 评论板块的制作,
看了这些却不知所云
- spam
- offensive
- disagree
- off topic
Like