WordPress 不消插件实现相干文章调取

时间:2014-01-26所属栏目: WordPress教程 来源: 热度:

早在 WEB2.0 时环绕着用户为主的网页体验里就呈现了 “您或者还喜好” 的字样,或者你只把它看成了“相干文章”的马甲来看,但这一改变确实大大拉进了页面和用户之间的间隔。那么在我们的 WordPress 站内怎样实现相干文章的调取呢?人神共知的谜底:用插件。那么对付那些对插件及处事器资源有“洁癖”的用户来说,我们必要不消插件也能调出相干文章的要领。

在 WordPress 中无需插件实现“您或者还喜好”

如上所述,我来先容一个简而有用的要领。这个要领是依据文章中的 tag 标签来举办匹配挪用的,由于 tag 比分类要有弹性,更利便我们来节制所谓的“同乐趣文章”。由此在行使上我们有很好的统筹搭配。

在 single.php 页面中插部下面代码:


<div id="maybe-you-like">
   <?php
	$backup = $post;
	$tags = wp_get_post_tags($post->ID);
	$tagIDs = array();
	if ($tags) {
	echo '<h3>您或者还喜好...</h3>';
	echo '<ul class="related-post">';
	$tagcount = count($tags);
	for ($i = 0; $i < $tagcount; $i++) {
	$tagIDs[$i] = $tags[$i]->term_id;
	}
	$args=array(
	'tag__in' => $tagIDs,
	'post__not_in' => array($post->ID),
	'showposts'=>6,
	'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() ?>"><?php the_title(); ?></a></li>
	<?php endwhile;
	echo '</ul>';
	} else { ?>
	<?php
	query_posts(array('orderby' => 'rand', 'showposts' => 6));
	if (have_posts()) :
	while (have_posts()) : the_post();?>
		<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
	<?php endwhile;endif; ?>
	<?php }
	}
	$post = $backup;
	wp_reset_query();
   ?>
</div>

这段代码是依据 TAG 来调取相干 6 篇文章,假如前提不符则随机调取 6 (showpost后的参数) 篇文章。颠末测试很好用,在不消插件的环境下可以说足够用了。

标签:
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)