WordPress高亮作者和文章置顶的实现
主题又折腾了一下,看起来舒服一点了。在折腾的途中,发现几个有趣的地方,一个是高亮作者评论,另外一个是文章置顶的实现。话说第一次在zww那儿看到,还不知道sticky post是个啥玩意……
一、高亮作者评论。但是发现很少人在用。
高亮作者,可以使用插件,这里不做讨论。用代码实现的话主要有两种方法。
第一种来自Matt Cutts,是比较古老了。
1.为主题添加一个“authcomment”样式来高亮作者评论
1 2 3 |
.authcomment { background-color: #B3FFCC !important; } |
2.编辑 comments.php 并加入一些代码
通常comments.php可能含有如下语句:
1 |
<li class=”<?php echo $oddcomment; ?>” id=”comment… |
通过一些修改变成如下样式:
1 2 3 4 5 6 |
<li class=”<?php /* Only use the authcomment class from style.css if the user_id is 1 (admin) */ if (1 == $comment->user_id) $oddcomment = “authcomment”; echo $oddcomment; ?>” id=”comment… |
这样就成功了。
————————————————————————————————–
另外一种可以直接在后台添加样式(需要登录后台才可以显示),遍历评论可以发现作者评论一般会含有类似class=”comment byuser comment-author-admin bypostauthor even depth-2″这样的样式,我们要做的就是添加一个comment-author-admin,让它显得与其他评论不一样,例如添加个背景图片、变一下颜色或者其他的。我是这么写的:
1 2 3 4 |
.children li.comment-author-admin span.avatarx{ background: #C04019; padding: 4px; } |
其中children是子评论,因为我发表的更多是回复,回复都在子评论里嵌套。Avatarx是评论显示的头像的样式,这样就实现了我这里的样子。
二、Sticky post。中文应该叫做文章置顶,从wp2.7开始出现。基本可以这样实现:
1.首先查看index.php中关于显示文章部分的代码。
比如默认主题中:
1 2 3 |
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> |
关键就是确定有这个语句。如果没有,例如类似这样:
1 2 3 4 |
<<?php if(have_posts()) : while(have_posts()) : the_post(); ?> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <?php the_content(); ?> <?php endwhile; endif; ?> |
你就可以改写一下:
1 2 3 4 5 6 |
<<?php if(have_posts()) : while(have_posts()) : the_post(); ?> <div <?php post_class(); ?>> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <?php the_content(); ?> </div> <?php endwhile; endif; ?> |
实际就是把文章内容圈一个div出来,把这个div加上一个,就像默认主题里那样。
2.css中定义sticky文章的样式,可以根据主题需要来定义。比如我的是这么写的:
1 2 3 4 5 |
#entry .sticky { padding:8px; background:url(images/sticky.png) top right no-repeat; border:1px solid #EEE; color:#000;} |
3.后台发布文章的时候,选择“置顶这篇文章到首页,这样就完成了。
PS:Post_class的用处很多。更多解释请参考:WordPress.org:post_class
置顶有了,不过样式还没有优化
高亮作者免了,不搞突出
@A.shun 我只是先试了试,以后用的时候再做做。毕竟我这里可以置顶的文章不多
大家都这么厉害…
原来封顶用插件的
@江流 只能说,WordPress的功能越来越强大了。
@阿修
我来测试一下,我的主题中,能够高亮作者评论,但是回复作者的评论也被高亮了~
@阿修
原来楼主只允许2层嵌套,要是多层嵌套,楼主高亮就弄不好了~~~
@logicmd 的确是这样的,嵌套的话判断起来比较麻烦
貌似我的博客讲过怎么给Wordpress模板加置顶功能》。。
@妖娆盛唐 呃?这个2.7就有了,你写过也很正常嘛~
技术文,顶。
没看明白。
你主题 的颜色 有点伤眼睛、、
《li class=”comment comment_author_email == get_the_author_email()) {echo ‘admin-comment’;} else {echo ‘guest-comment’;} ?>” id=”comment-“》
在某个主题的 functions.php 里 发现的,可以作为 判定、
先试试再说
求教 如何设置只有置顶的文章才显示文章的特色图片
@刘来祥 可以用函数:if_sticky
{
显示特色图片
}
不错,正好需要这个高亮效果~
这样免得别人冒充作者