You are here: Home » All Categories » Play WordPress » willin的相关文章代码

willin的相关文章代码

Jun 5th, 2010 11:18 | Leave a comment?(70) Go to comments

从网上搜来的相关文章代码有个弊病,就是代码放在网页某些地方(如放在文章内容后面)会搞乱 $post 的顺序,从而会影响一些如评论调用错乱的问题,我也因为此而一直用插件来实现这个功能。

低调高手 willin 在其文章《A9 主题的特色》里面贴出了 A9 主题所用的相关文章代码,代码由 willin 一手打造,很好用。

这段代码有2个与众不同的地方:
1. 先取每个 tags 的文章,当 tags 文章数量不足, 再取 category 补足。
2. 使用 $post 暂存, 代码放在网页任何地方都不不会搞乱 $post 顺序。

使用方法很简单,直接将下面的代码复制到需要使用的地方即可:

<h3>相关文章:</h3>
<ul>
<?php
$post_num = 5; // 设置相关文章数量.
global $post;
$tmp_post = $post;
$tags = ''; $i = 0; // 先取 tags 文章.
if ( get_the_tags( $post->ID ) ) {
foreach ( get_the_tags( $post->ID ) as $tag ) $tags .= $tag->name . ',';
$tags = strtr(rtrim($tags, ','), ' ', '-');
$myposts = get_posts('numberposts='.$post_num.'&tag='.$tags.'&exclude='.$post->ID);
foreach($myposts as $post) {
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
$i += 1;
}
}
if ( $i < $post_num ) { // tags 文章数量不足, 再取 category 补足.
$post = $tmp_post; setup_postdata($post);
$cats = ''; $post_num -= $i;
foreach ( get_the_category( $post->ID ) as $cat ) $cats .= $cat->cat_ID . ',';
$cats = strtr(rtrim($cats, ','), ' ', '-');
$myposts = get_posts('numberposts='.$post_num.'&category='.$cats.'&exclude='.$post->ID);
foreach($myposts as $post) {
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
}
}
$post = $tmp_post; setup_postdata($post);
?>
</ul>

贴完。

2010.6.6 Edit:

1. 评论中阿修kisa747指出“如果文章没有标签那么会出错”,这个问题阿修在评论里给出临时解决办法(已添加进上面的代码)【willin 已经修正】

2. 由于 willin 的源代码贴出错误,致使相关文章变成最新文章,我已经修正。

请在 2010.6.6 13:33 前 CP(Copy Paste) 代码的朋友重新 CP 使用。

2010.11.29 Update: 最新的代码请移步此 http://zww.me/archives/25353

声明: 除非注明,ZWWoOoOo文章均为原创,转载请以链接形式标明本文地址
本文地址: http://zww.me/archives/25166

Filed under

Play WordPress

| Tags:

,

Related Posts

Most Popular

70 Comments.

⊕Leave a comment?
  1. ifenwen ifenwen Google Chrome 5.0.375.55Windows XP

    也不知道自己用的谁的代码了

    沙发!软软很舒服!1
  2. winy winy Firefox 3.6.3Windows 7

    我是都拿来参考,自己修改的合适

    板凳!有点硬!2
  3. Max Lee Max Lee Google Chrome 5.0.375.55Windows 7

    拿来试试,感谢分享~

    地板!又硬又冷!3
  4. TTkea TTkea Firefox 3.6.3Windows 7

    技术文MK~~ :razz:

    4楼
  5. jalena jalena Internet Explorer 6.0Windows XP

    哇,IE6下评论框的位置和广告为有重叠哦!

    5楼
  6. A.shun A.shun Opera 10.60Windows XP

    我用的代码是直接从WordPress Related Posts中挖出来的

    6楼
  7. 包子 包子 Opera 10.53Windows XP

    收藏一下.

    7楼
  8. 阿修 阿修 Google Chrome 5.0.375.55Windows XP

    发现个问题,如果文章没有标签,会报错的

    8楼
  9. 囧笑娱乐 囧笑娱乐 Firefox 3.6.3Windows 7

    14行开始后面多了一个?

    9楼
  10. 集趣 集趣 Google Chrome 5.0.360.4Windows XP

    是不是利用SQL也可以实现这样的效果呢?利用文章标题的相似度

    10楼
  11. 阿邙 阿邙 ChromePlus 1.3.9.0Windows 7

    这个不错 呵呵 有没有合适的摘要代码分享下.

    11楼
  12. 疾风 疾风 Firefox 3.6.3Windows XP

    感觉我现在用的还凑合 就不换了~

    12楼
  13. Dianso Dianso Google Chrome 5.0.375.55Windows 7

    用了,不过每个文章前都有个?

    13楼
  14. Dianso Dianso Google Chrome 5.0.375.55Windows 7

    好了,删除代码里2个问号就行了

    14楼
    • zwwooooo zwwooooo Firefox 3.6.3Windows 7

      @Dianso 嗯,willin那里贴错了,我自个用的更正了,但我发文章时又copy willin的了,囧~

  15. 猪八戒 猪八戒 Firefox 3.6.3Windows XP

    看不懂代码啊,什么时候能步入你们高手的行列呢?这样提示点广告不怕百度处罚呀?

    15楼
  16. kisa747 kisa747 Google Chrome 5.0.375.53Windows 7

    这个代码自动补足分类下的文章比较好。

    16楼
  17. mice mice Internet Explorer 8.0Windows 7

    o(︶︿︶)o 还是用插件好了,理解WP不够透彻慢慢折腾吧..

    17楼
  18. kisa747 kisa747 Firefox 3.6.3Windows 7

    如果文章没有tags会报错,建议加个if (get_the_tags()) 先判断下

    18楼
  19. ikeeptrying ikeeptrying Google Chrome 6.0.422.0Windows 7

    友情支持~~

    19楼
  20. 万戈 万戈 Firefox 3.6.3Windows XP

    我用的就是这个方法耶,呵呵

    20楼

Leave a Reply


Trackbacks and Pingbacks: