Categorized | 网络技术

Tags |

wordpress相关文章代码增强版

发布于 2012年07月14日 更新于 2012年07月14日 

wordpress的相关文章插件有很多,直接在后台搜索就能发现不少,不过插件的安装是必会造成网站速度的下降,YARPP插件(YARPP全称:yet another related posts)评论不错,不过我这博客还是打算用代码来实现相关功能。

wordpress相关文章核心代码

<?php
$post_tags=wp_get_post_tags($post->ID); //如果存在tag标签,列出tag相关文章
$pos=1;
if($post_tags) {
foreach($post_tags as $tag) $tag_list[] .= $tag->term_id;
$args = array(
‘tag__in’ => $tag_list,
‘category__not_in’ => array(NULL), // 不包括的分类ID,可以把NULL换成分类ID
‘post__not_in’ => array($post->ID),
‘showposts’ => 0, // 显示相关文章数量
‘caller_get_posts’ => 1,
‘orderby’ => ‘rand’
);
query_posts($args);
if(have_posts()):while (have_posts()&&$pos<=10) : the_post(); update_post_caches($posts); ?>
<li><span><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php if(get_the_title())the_title();else the_time(‘Y-m-d’); ?></a></span> <span><?php the_time(‘Y-m-d’) ?> <?php the_category(‘/’,”) ?></span></li>
<?php $pos++;endwhile;wp_reset_query();endif; ?>
<?php } //end of rand by tags ?>
<?php if($pos<=10): //如果tag相关文章少于10篇,那么继续以分类作为相关因素列出相关文章
$cats = wp_get_post_categories($post->ID);
if($cats){
$cat = get_category( $cats[0] );
$first_cat = $cat->cat_ID;
$args = array(
‘category__in’ => array($first_cat),
‘post__not_in’ => array($post->ID),
‘showposts’ => 0,
‘caller_get_posts’ => 1,
‘orderby’ => ‘rand’
);
query_posts($args);
if(have_posts()): while (have_posts()&&$pos<=10) : the_post(); update_post_caches($posts); ?>
<li><span><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title_attribute();?>”><?php if(get_the_title())the_title();else the_time(‘Y-m-d’); ?></a></span> <span><?php the_time(‘Y-m-d’);if($options[‘tags’]):the_tags(”, ‘/’, ”);endif; ?></span></li>
<?php $pos++;endwhile;wp_reset_query();endif; ?>
<?php } endif; //end of rand by category ?>
<?php if($pos<=10){ //如果上面两种相关都还不够10篇文章,再随机挑几篇凑成10篇 ?>
<?php query_posts(‘showposts=10&orderby=rand’);while(have_posts()&&$pos<=10):the_post(); ?>
<li><span><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php if(get_the_title())the_title();else the_time(‘Y-m-d’) ?></a></span> <span>[ <?php the_category(‘/’,”); ?>]</span></li>
<?php $pos++;endwhile;wp_reset_query();?>
<?php } ?>

代码非常好用,而且是带随机功能的,对于网站的更新以及内部链接建设都很不错,推荐使用,效果可见本博客!

 

看了此文的人貌似还看了这些:

  • wordpress如何给read more和comment评论链接添加nofollow? 2013-08-26
  • wordpress换空间实录,让windows的iis也支持去固定链接index.php化! 2011-02-21
  • 浏览器输入淘宝首页会自动跳转到淘宝特卖频道,何解? 2012-07-14
  • wordpress换空间实录,让windows的iis也支持去固定链接index.php化! 2011-02-21
  • linux系统禁ping及改ssh端口 2017-10-25
  • Amazon EC2的V-P-N-PPTP搭建教程(on aws redhat6.5 X64) 2014-10-20
  • centos安装ss多用户带前端教程 2015-02-03
  • Microsoft Office 2010 Pro Plus专业增强版VOL安装教程 2012-09-03
  • 易语言curl支持库与WinHttpRequest、WinInet、互联网支持库http访问对比测试 2014-04-30
  • VBA宏命令:批量检索替换/修改word中指定多个关键词的格式内容等 2015-04-11
  • 3 Comments For This Post

    1. simon Says:

      测试一下评论

    2. commodity mould Says:

      以后可以用的倒,现在暂时还不行

    3. 科技盒子www.kejihezi.com Says:

      怎么去掉重复的文章呢

    1 Trackbacks For This Post

    1. wordpress相关文章代码增强版_独善其身 Says:

      […] 原文链接:http://sunweiwei.com/700/ […]

    发表评论

    Time limit is exhausted. Please reload CAPTCHA.

       我相信你,不会发广告!

    斗牛SEO工具