您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程
WordPress主题制作之怎么制作comments.php文件
梦柏2023-10-31WordPress教程已有人查阅
导读今天我们来制作评论主题的评论模块。在主题目录Aurelius下新建comments.php,在single.php剪切以下代码,粘贴到comments.php:
今天我们来制作评论主题的评论模块。在主题目录Aurelius下新建comments.php,在single.php剪切以下代码,粘贴到comments.php:
为了安全起见,不让恶意用户直接打开评论文件,请在comments.php头部添加以下代码:
函数名称函数功能get_avatar($comment, 48)获取评论者的gravatar头像,尺寸为48 * 48comment_reply_link()回复留言的链接get_comment_author_link用于获取评论者博客地址get_comment_time获取评论发布时间edit_comment_link管理员修改评论的链接comment_text()输出评论内容
好,现在在你的文章页面底部就可以正常地显示评论了!现在我们继续来制作提交评论的表单,将以下代码删除(也就是评论表单的代码):
如果该用户之前已经发表过评论则自动帮助用户填写Email$comment_author_url读取cookie,如果该用户之前已经发表过评论则自动帮助用户填写博客地址do_action('comment_form', $post->ID);该函数为某些插件预留wp_logout_url退出登录的链接
<!– Comment’s List –>
<h3>Comments</h3>
<div class="hr dotted clearfix"> </div>
<ol class="commentlist">
<li class="comment">
<div class="gravatar"> <img alt="" src=’images/gravatar.png’ height=’48′ width=’48′ /> <a class="comment-reply-link" href=">Reply</a> </div>
<div class="comment_content">
<div class="clearfix"> <cite class="author_name"><a href="">Joe Bloggs</a></cite>
<div class="comment-meta commentmetadata">January 6, 2010 at 6:26 am</div>
</div>
<div class="comment_text">
<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>
</div>
</div>
</li>
</ol>
<div class="hr clearfix"> </div>
<!– Comment Form –>
<form id="comment_form" action="" method="post">
<h3>Add a comment</h3>
<div class="hr dotted clearfix"> </div>
<ul>
<li class="clearfix">
<label for="name">Your Name</label>
<input id="name" name="name" type="text" />
</li>
<li class="clearfix">
<label for="email">Your Email</label>
<input id="email" name="email" type="text" />
</li>
<li class="clearfix">
<label for="email">Your Website</label>
<input id="website" name="website" type="text" />
</li>
<li class="clearfix">
<label for="message">Comment</label>
<textarea id="message" name="message" rows="3" cols="40"></textarea>
</li>
<li class="clearfix">
<!– Add Comment Button –>
<a type="submit" class="button medium black right">Add comment</a> </li>
</ul>
</form>
在single.php原位置添加代码:
<?php comments_template(); ?>
以上语句的作用就是将comments.php里的所有内容导入到single.php中,与直接在single.php写comments.php中的代码效果是一样的。为了安全起见,不让恶意用户直接打开评论文件,请在comments.php头部添加以下代码:
<?php
if (isset($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
('Please do not load this page directly. Thanks!');
?>
因为WordPress的输出评论函数wp_list_comments()输出的评论代码与我们主题的评论代码不一样的,我们得自定义我们的评论列表,将comments.php中的以下代码删除(以下代码用于列出文章的所有评论):
<li class="comment">
<div class="gravatar"> <img alt="" src=’images/gravatar.png’ height=’48′ width=’48′ /> <a class="comment-reply-link" href=">Reply</a> </div>
<div class="comment_content">
<div class="clearfix"> <cite class="author_name"><a href="">Joe Bloggs</a></cite>
<div class="comment-meta commentmetadata">January 6, 2010 at 6:26 am</div>
</div>
<div class="comment_text">
<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>
</div>
</div>
</li>
改成:
<?php
if (!empty($post->post_password) && $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) {
// if there's a password
// and it doesn't match the cookie
?>
<li class="decmt-box">
<p><a href="#addcomment">请输入密码再查看评论内容.</a></p>
</li>
<?php
} else if ( !comments_open() ) {
?>
<li class="decmt-box">
<p><a href="#addcomment">评论功能已经关闭!</a></p>
</li>
<?php
} else if ( !have_comments() ) {
?>
<li class="decmt-box">
<p><a href="#addcomment">还没有任何评论,你来说两句吧</a></p>
</li>
<?php
} else {
wp_list_comments('type=comment&callback=aurelius_comment');
}
?>
以上代码的意思大致也可以看得出来了,就是一大堆 如果...就....,如果以上条件都不满足就列出所有评论。现在将主题文件夹Aurelius中的functions.php中的 ?> ,改成以下代码,如果你之前从本博客下载到的functions.php已经有以下代码则不用再添加:
function aurelius_comment($comment, $args, $depth)
{
$GLOBALS['comment'] = $comment; ?>
<li class="comment" id="li-comment-<?php comment_ID(); ?>">
<div class="gravatar"> <?php if (function_exists('get_avatar') && get_option('show_avatars')) { echo get_avatar($comment, 48); } ?>
<?php comment_reply_link(array_merge( $args, array('reply_text' => '回复','depth' => $depth, 'max_depth' => $args['max_depth']))) ?> </div>
<div class="comment_content" id="comment-<?php comment_ID(); ?>">
<div class="clearfix">
<?php printf(__('<cite class="author_name">%s</cite>'), get_comment_author_link()); ?>
<div class="comment-meta commentmetadata">发表于:<?php echo get_comment_time('Y-m-d H:i'); ?></div>
<?php edit_comment_link('修改'); ?>
</div>
<div class="comment_text">
<?php if ($comment->comment_approved == '0') : ?>
<em>你的评论正在审核,稍后会显示出来!</em><br />
<?php endif; ?>
<?php comment_text(); ?>
</div>
</div>
<?php } ?>
以上代码所用到的WordPress函数及相应的说明:函数名称函数功能get_avatar($comment, 48)获取评论者的gravatar头像,尺寸为48 * 48comment_reply_link()回复留言的链接get_comment_author_link用于获取评论者博客地址get_comment_time获取评论发布时间edit_comment_link管理员修改评论的链接comment_text()输出评论内容
好,现在在你的文章页面底部就可以正常地显示评论了!现在我们继续来制作提交评论的表单,将以下代码删除(也就是评论表单的代码):
<!– Comment Form –>
<form id="comment_form" action="" method="post">
<h3>Add a comment</h3>
<div class="hr dotted clearfix"> </div>
<ul>
<li class="clearfix">
<label for="name">Your Name</label>
<input id="name" name="name" type="text" />
</li>
<li class="clearfix">
<label for="email">Your Email</label>
<input id="email" name="email" type="text" />
</li>
<li class="clearfix">
<label for="email">Your Website</label>
<input id="website" name="website" type="text" />
</li>
<li class="clearfix">
<label for="message">Comment</label>
<textarea id="message" name="message" rows="3" cols="40"></textarea>
</li>
<li class="clearfix">
<!– Add Comment Button –>
<a type="submit" class="button medium black right">Add comment</a> </li>
</ul>
</form>
改成:
<?php
if ( !comments_open() ) :
// If registration required and not logged in.
elseif ( get_option('comment_registration') && !is_user_logged_in() ) :
?>
<p>你必须 <a href="<?php echo wp_login_url( get_permalink() ); ?>">登录</a> 才能发表评论.</p>
<?php else : ?>
<!-- Comment Form -->
<form id="commentform" name="commentform" action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post">
<h3>发表评论</h3>
<div class="hr dotted clearfix"> </div>
<ul>
<?php if ( !is_user_logged_in() ) : ?>
<li class="clearfix">
<label for="name">昵称</label>
<input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="23" tabindex="1" />
</li>
<li class="clearfix">
<label for="email">电子邮件</label>
<input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="23" tabindex="2" />
</li>
<li class="clearfix">
<label for="email">网址(选填)</label>
<input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="23" tabindex="3" />
</li>
<?php else : ?>
<li class="clearfix">您已登录:<a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="退出登录">退出 »</a></li>
<?php endif; ?>
<li class="clearfix">
<label for="message">评论内容</label>
<textarea id="message comment" name="comment" tabindex="4" rows="3" cols="40"></textarea>
</li>
<li class="clearfix">
<!-- Add Comment Button -->
<a href="javascript:void(0);" onClick="Javascript:document.forms['commentform'].submit()" class="button medium black right">发表评论</a> </li>
</ul>
<?php comment_id_fields(); ?>
<?php do_action('comment_form', $post->ID); ?>
</form>
<?php endif; ?>
函数名称函数功能is_user_logged_in判断用户是否登录wp_login_url博客登录地址get_comment_author_link用于获取评论者博客地址$comment_author读取cookie,如果该用户之前已经发表过评论则自动帮助用户填写用户名$comment_author_email读取cookie;如果该用户之前已经发表过评论则自动帮助用户填写Email$comment_author_url读取cookie,如果该用户之前已经发表过评论则自动帮助用户填写博客地址do_action('comment_form', $post->ID);该函数为某些插件预留wp_logout_url退出登录的链接
本文标签:
很赞哦! ()
相关教程
图文教程
WordPress插件模板有哪些
丰富的插件和模板是WordPress非常流行的一个特性。WordPress插件数据库中有超过18000个插件,包括SEO、控件等等。
WordPress文章摘要显示和修改方法
我目前另一WordPress博客就是使用这种方法的,显示效果也很满意, 不足就是写文章时需单独设置摘要。不过这点也要辩证来看,虽然是麻烦了点,但灵活性好。
CentOS7安装WordPress的步骤方法
在开始本文前,我假定你已经安装好了nginx、php-fpm和mariaDB(或mysql)。它们的安装过程可参考我以前的文章。
wordpress安装配置教程
1.关闭防护墙2.配制主从ip3.配置yum源4.安装软件包mysql_secure_installation创建站点目录并移进相关文件
相关源码
-
(自适应响应式)双语LED照明灯饰灯具外贸网站pbootcms源码下载模板采用响应式设计,能自动适应手机、平板和电脑等多种设备屏幕,确保用户在不同设备上都能获得良好的浏览体验。同一后台管理,数据实时同步,操作简便高效。查看源码 -
(PC模板)工商公司注册会计财务记账pbootcms模板源码下载基于PbootCMS的工商财税行业网站系统,手工编写前端代码确保执行效率,双端自适应设计,支持后台实时更新服务价格和政策文件。查看源码 -
(自适应响应式)房产合同知识产权企业管理pbootcms模板下载本模板基于PbootCMS系统开发,为知识产权服务、法律咨询及企业合同管理等行业设计。采用严谨专业的布局风格,突出法律文书与知识产权服务行业特色,适合展示各类法律服务和知识产权相关内容。查看源码 -
(自适应)蓄电池能源智能数码科技产品pbootcms模板源码下载本款基于PbootCMS开发的网站模板为蓄电池及能源科技企业设计,特别适合锂电池、储能系统、新能源电池等产品的展示与推广。查看源码 -
帝国cms7.5模板情感文学名言名句心情文章类源码下载带手机本模板基于帝国CMS7.5开发,为情感文学类网站设计。整体风格温馨雅致,布局合理清晰,特别适合建设情书分享、文学作品展示类网站。模板采用响应式设计,能够自动适配各种终端设备。查看源码 -
(自适应)绿色新闻生活百科资讯文章博客类网站pbootcms模板源码本模板基于PbootCMS开发,为生活百科、资讯文章和博客类网站设计。采用清新绿色系风格,提供舒适的阅读体验,同时适配PC和移动设备。适用于生活技巧分享、健康知识传播查看源码
| 分享笔记 (共有 篇笔记) |

