WordPress 用户应该都知道,每篇文章都可加入标签,方便读者找到题材相近的文章。如果可以在网页上显示这些文章的标题和连结,制作成一个列表,相信对读者很有帮助,亦可增加网志的 Page View。笔者以前介绍过用分类 (Category) 制作「相关文章」列表,今次改用标签来做,而另一个不同之处是把这列表放在侧栏上。放在侧栏和放在文章内容下方有什么不同呢?答案是绝大部分 Template 中,前者是 The Loop 完结后才运行,而很多 template tags 是只可用在 The Loop之中,如果你发现以前修改 template 时常常遇到问题,很可能是没注意到这问题,故乱把内文一些元素移到侧栏之上。
- <?php
- global$post;
- $alltags = ”;
- $posttags = get_the_tags($post->ID);
- if ($posttags) {
- foreach($posttags as $tag) {
- $alltags . $tag->name . ‘,’;
- }
- }
- ?>
- <?php
- $args = array(
- ‘numberposts’ => 5,
- ‘post__not_in’ => array($post->ID),
- ‘orderby’=>‘rand’,
- ‘tag’ => $alltags
- );
- $myposts = get_posts($args);
- foreach($myposts as $post) {
- echo‘<li><a href="’;
- the_permalink();
- echo‘">’;
- the_title();
- echo‘</a></li>’;
- }
- ?>
为了确保您获得理想的网页设计服务,深圳网站建设公司-沙漠风(www.szweb.cn.),设计团队为您提供FLASH网站设计,网页制作,多媒体触摸屏展示设计.