继续折腾-无插件实现 wordpress 相关文章功能
之前不知道在哪里看到的关于 wordpress 相关文章的插件介绍,最后选择了yet another related posts plugin 这款相关文章插件。可能是自己设置得不够好,所以在很多文章里看到的相关文章都特别地少,偶尔气愤一下,同个关键词,也相关不到。既然追求速度,插件能少一个是一个。想办法无插件实现 wordpress 相关文章功能,在网上找到万戈老大的文章《WordPress 非插件相关日志的两种方法》,容易简单,但功能基本能满足博客基本的要求,暂时不考虑Feed的输出,所以决定采用其中的第2种。开始折腾吧。
仅在单篇日志中显示相关日志。
在 WordPress 主题文件 single.php 中需要的位置插入以下代码即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <h3>相关日志</h3> <ul> <?php $tags = wp_get_post_tags($post->ID); if ($tags) { $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>10, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title();?> <?php comments_number(' ','(1)','(%)'); ?></a></li> <?php endwhile; } } wp_reset_query(); ?> </ul> |
并在这段代码包加< div id=”related_posts”> < /div>,直接调inove主题下载的相关文章样式,当然可以根据自己的需要修改。
至于,单篇日志和 feed 中都可以生成相关日志,有需要的童鞋可以去万戈那里看看。
转载请注明 转自: 迷走映像丨继续折腾-无插件实现 wordpress 相关文章功能
哈哈,不知道站长大几了啊
[Reply]
alexin Reply:
August 29th, 2010 at 19:34
@sky, 9月份开始,研一啊。
[Reply]
太棒了,要的就是这个!爱死你了,亲一个……
[Reply]
不用插件最好,安装很多插件会把网站拖跨
[Reply]