相关文章

<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>

via:http://zww.me/archives/25166

/* Related Posts 函数:出自kan.willin.org,函数修改zww.me */
function RelatedPosts($post_num = 5) {
	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="'.the_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);} ?>

{ 发表评论? }

  1. 匿名

    014.cc的文章总是很实在、很中肯,不浮夸、不造作。每次来都能学到很多东西。在牛皮漫天飞的互联网上,能见到014.cc这样实实在在、中规中矩的网站实在不多了

发表评论

电子邮件地址不会被公开。

 smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles smiles

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

无觅相关文章插件,快速提升流量