willin版相关文章代码写成函数

» 2010-09-01 80条评论

这个是一个朋友需要的,顺便贴出来。

对于熟悉php的盆友这很简单,但还是有很多盆友不懂的,但CP(CopyPaste)肯定懂……但……没有但了……

直接使用的代码可以到这里CP《willin的相关文章代码》(老代码)

或者去willin那里《A9 主题的特色

上面纯粹废话,目的是一定的文章长度才能配合好左边小张的广告图片,哇咔咔

好,废话完,正题!

把下面的代码扔进主题的 functions.php 里面:
2010.11.29 Update:下面这段代码有些小bug,具体移步到这里 https://zww.me/archives/25353 ,根据新的代码对比修正——替换第5行~11行相近的内容)

/* Related Posts 函数:出自kan.willin.org,函数修改zww.me */
function RelatedPosts($post_num = 5) { global $post;
	if(is_single()) {//只在文章页使用
		$related_posts = '<h3>Related Posts:</h3><ul>';
		$exclude_id = $post->ID; // 單獨使用要開此行
		$posttags = get_the_tags(); $i = 0;
		if ( $posttags ) { $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->name . ',';
		$args = array(
			'post_status' => 'publish',
			'tag_slug__in' => explode(',', $tags), // 只選 tags 的文章.
			'post__not_in' => explode(',', $exclude_id), // 排除已出現過的文章.
			'caller_get_posts' => 1,
			'orderby' => 'comment_date', // 依評論日期排序.
			'posts_per_page' => $post_num
		);
		query_posts($args);
		 while( have_posts() ) { the_post();
			$related_posts .= '<li><a href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></li>';
			$exclude_id .= ',' . $post->ID; $i ++;
		 } wp_reset_query();
		}
		if ( $i < $post_num ) { // 當 tags 文章數量不足, 再取 category 補足.
		$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
		$args = array(
			'category__in' => explode(',', $cats), // 只選 category 的文章.
			'post__not_in' => explode(',', $exclude_id),
			'caller_get_posts' => 1,
			'orderby' => 'comment_date',
			'posts_per_page' => $post_num - $i
		);
		query_posts($args);
		 while( have_posts() ) { the_post();
			$related_posts .= '<li><a href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></li>';
		 } wp_reset_query();
		}
		$related_posts .= '</ul>';
		return $related_posts;
	}
}

然后用这个调用:

<?php if(function_exists('RelatedPosts')){RelatedPosts(5);} ?>

(里面的5就是显示相关的文章数量)

PS:其实那位折腾的朋友不是这样调用的,加了个自动插入相关文章到文章页面和feed输出代码,就是再把下面这段代码扔functions.php:

function insertfooter($content) {
	if( is_single() || is_feed()) {//文章页和feed输出
		$content.= RelatedPosts(5);//文章结尾插入相关文章
	}
	return $content;
}
add_filter ('the_content', 'insertfooter');

完了。- -

zww
or
oooo

“willin版相关文章代码写成函数”有80条评论

  1. 直接代码的路过....

    1. zwwooooo says:

      @掌柜的马甲 函数多了个调用

  2. 园子 says:

    开始的话很多
    结尾来个急刹车
    哈哈。

    1. zwwooooo says:

      @园子 所以说前面是废话嘛

  3. 久酷 says:

    貌似这样不带缓存功能,每打开一篇文章都要执行一次……除非装了生成静态页面的插件!

    1. zwwooooo says:

      @久酷 这样么?不太清楚php的执行

  4. 球犯 says:

    和《willin的相关文章代码》效果是一样的吗?

    1. zwwooooo says:

      @球犯 本来就是,只是插入方式不同

    2. 球犯 says:

      @zwwooooo 哦 原来这样 学习了~

  5. mice says:

    :mrgreen: zww千秋万载..你懂的..

  6. Xshagua says:

    暂时还不需要,主题自带

    1. zwwooooo says:

      @Xshagua 那就不用折腾了

  7. Timothy says:

    代码和插件,我还是偏向插件。这样以后升级wordpress的时候,不用手动去改funcions.php了……

    1. zwwooooo says:

      @Timothy 其实在functions.php里面的代码一般也不需要动,除非你换主题

  8. 老饕 says:

    add_filter~~最近用了这个钩子~~好用啊!

    1. zwwooooo says:

      @老饕 对啊,这个非常方便,我不太会

  9. 书香阁 says:

    虽然不是很懂 支持下博主辛苦的分享精神

  10. FORECE says:

    我直接用的水煮鱼的。。。

    1. zwwooooo says:

      @FORECE 插件简单方便

  11. 疾风 says:

    我的functions都很庞大了 呵呵

    1. zwwooooo says:

      @疾风
      相当于把插件功能都扔进了functions.php,呵呵

  12. 郑永 says:

    貌似能用到,我还在想为什么大家的feed里面都有相关文章,我的插件也应该支持这个啊。汗。现在还是用代码吧。

  13. 还是用插件来的方便

  14. ylsnuha says:

    为什么调用不能用?。。。

    1. zwwooooo says:

      @ylsnuha
      检查一下是否哪里出错了

  15. ylsnuha says:

    请问怎么让相关日志和随机日志并排显示呢 具体的代码 比如我现在2个函数输出

    
    
    				
    1. zwwooooo says:

      @ylsnuha
      用左右浮动呗。
      float:left;
      float:right;
      具体代码看我的博客文章末尾,用firebar看

  16. ylsnuha says:

    为什么我的相关文章总是包含原文章呢。。。
    那个 //單獨使用要開此行 那行开了也不行

    1. 浩子 says:

      我也不行呢?
      也有原文章,怎么回事呢?

    2. ylsnuha says:

      @浩子
      感觉好像是因为相关文章比较少才会出现的 如果有的相关程度比较高的就不会出现了 我貌似有的文章出现原文有的没有 可是怎么取消出现原文就不知道了

    3. zwwooooo says:

      @ylsnuha
      这是willin写的,抽时间看看,貌似我的很少出现。

    4. zwwooooo says:

      @浩子
      willin写的,具体得看看

  17. Kars says:
    function related_posts() {
    global $post;
    $post_num = 5;
    $exclude_id = $post->ID;
    $posttags = get_the_tags(); $i = 0;
    $related_posts = "";
    if ( $posttags ) {
    	$tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
    	$args = array(
    		'post_status' => 'publish',
    		'tag__in' => explode(',', $tags),
    		'post__not_in' => explode(',', $exclude_id),
    		'caller_get_posts' => 1,
    		'orderby' => 'comment_date',
    		'posts_per_page' => $post_num
    	);
    	query_posts($args);
    	while( have_posts() ) { the_post();
    		$related_posts .= '<li><a rel="bookmark" href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></li>';
    		$exclude_id .= ',' . $post->ID; $i ++;
    	} wp_reset_query();
    }
    if ( $i < $post_num ) {
    	$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
    	$args = array(
    		'category__in' => explode(',', $cats),
    		'post__not_in' => explode(',', $exclude_id),
    		'caller_get_posts' => 1,
    		'orderby' => 'comment_date',
    		'posts_per_page' => $post_num - $i
    	);
    	query_posts($args);
    	while( have_posts() ) { the_post(); 
    		$related_posts .= '<li><a rel="bookmark" href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></li>';
    		$exclude_id .= ',' . $post->ID; $i ++;
    	} wp_reset_query();
    }
    if ( $i < $post_num ) {
    	$args = array(
    		'post__not_in' => explode(',', $exclude_id),
    		'caller_get_posts' => 1,
    		'orderby' => 'rand',
    		'posts_per_page' => $post_num - $i
    	);
    	query_posts($args);
    	while( have_posts() ) { the_post(); 
    		$related_posts .= '<li><a rel="bookmark" href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></li>';
    		$exclude_id .= ',' . $post->ID; $i ++;
    	} wp_reset_query();
    }
    if ( $i  == 0 ) $related_posts .= '<li>No Related Articles!</li>';
    return $related_posts;
    }

    你漏了 global $post; ,会导致当前文章也被加入了相关文章里、、

    1. zwwooooo says:

      @Kars
      忘了加,谢谢指正。

    2. Kars says:

      @zwwooooo
      自己打算 往Feed里 加 相关文章 和 Copyright,顺便把 Ozh Better Feed 这插件关了、

    3. zwwooooo says:

      @Kars
      很多插件功能都可以扔functions.php,哈

    4. Kars says:

      @zwwooooo
      但也有些 不能扔、、 :cry:

    5. zwwooooo says:

      @Kars
      当然,哈哈,其实你如果直接把插件代码扔到functions.php,倒不如用插件吧。

    6. Kars says:

      @zwwooooo
      总归是 简化后 再扔,反正是 自己用,有些地方 倒不如直接 写死~ :mrgreen:

  18. Arks Absolute Area says:

    在 Feed (RSS) 中 插入 相关文章 以及 版权声明...

    如果 要向 Feed 中 插入 相关文章,那就要 先把 上篇文章 中的 相关文章的代码 写成 函数、、 不想自己动手的懒人们,就自己动手 复制粘贴 我写好的代码 至 当前主题目录中的 functions.php 中吧、 相关文章 函数 代码 如下: <?php function related_posts() { global $post; $post_num = 5; $exclude_id = $post->ID; $posttags = get_the_tags(); $i = 0...

  19. 谢谢指导., 我市直接贴进去的,没放functions.php里

回复给 久酷 ¬
取消回复

昵称 *

网址

B em del U Link Code Quote