鱼见海资源网最新教程:
今天我们来制作评论主题的评论模块。在主题目录Aurelius下新建comments.php,在single.php剪切以下代码,粘贴到comments.php:
Comments
在single.php原位置添加代码:
以上语句的作用就是将comments.php里的所有内容导入到single.php中,与直接在single.php写comments.php中的代码效果是一样的。
为了安全起见,不让恶意用户直接打开评论文件,请在comments.php头部添加以下代码:
因为WordPress的输出评论函数wp_list_comments()输出的评论代码与我们主题的评论代码不一样的,我们得自定义我们的评论列表,将comments.php中的以下代码删除(以下代码用于列出文章的所有评论):
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.
改成:
post_password) && $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) {
// if there's a password
// and it doesn't match the cookie
?>
以上代码的意思大致也可以看得出来了,就是一大堆 如果…就….,如果以上条件都不满足就列出所有评论。现在将主题文件夹Aurelius中的functions.php中的 ?> ,改成以下代码,如果你之前从本博客下载到的functions.php已经有以下代码则不用再添加:
function aurelius_comment($comment, $args, $depth)
{
$GLOBALS['comment'] = $comment; ?>
'回复','depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
%s'), get_comment_author_link()); ?>
comment_approved == '0') : ?>
你的评论正在审核,稍后会显示出来!
以上代码所用到的WordPress函数及相应的说明:
函数名称 函数功能
get_avatar($comment, 48) 获取评论者的gravatar头像,尺寸为48 * 48
comment_reply_link() 回复留言的链接
get_comment_author_link 用于获取评论者博客地址
get_comment_time 获取评论发布时间
edit_comment_link 管理员修改评论的链接
comment_text() 输出评论内容
好,现在在你的文章页面底部就可以正常地显示评论了!现在我们继续来制作提交评论的表单,将以下代码删除(也就是评论表单的代码):
改成:
你必须 登录 才能发表评论.
函数名称 函数功能
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 退出登录的链接
好了,评论模板comments.php制作完毕!
文章来源于互联网
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.