我们在制造wordpress主题的时刻常常会为文章模板增添一些相干文章的功用雄厚,他们有的时刻出如今侧栏,有的时刻出如今文章的底部相干文章这块,固然wordpress相干文章的插件也有许多,然则为了这么个小功用去用插件就有点牛鼎烹鸡了,实在我们可以经由过程代码完成如许的一个小功用!
经由过程增添wordpress相干文章的功用,可以让访客更多的阅读我们的网站,从而增添PV,友爱体验!
下面我们就排列几个完成wordpress相干文章的几种要领:
要领一:标签相干
起首猎取文章的一切标签,接着猎取这些标签下的 n 篇文章,那末这 n 篇文章就是与该文章相干的文章了。如今可以见到的wordpress相干文章插件都是运用的这个要领。下面是完成的代码:
<ul id="tags_related">
<?php
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
foreach ($post_tags as $tag) {
// 猎取标签列表
$tag_list[] .= $tag->term_id;
}
// 随机猎取标签列表中的一个标签
$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
// 该要领运用 query_posts() 函数来挪用相干文章,以下是参数列表
$args = array(
'tag__in' => array($post_tag),
'category__not_in' => array(NULL), // 不包括的分类ID
'post__not_in' => array($post->ID),
'showposts' => 6, // 显现相干文章数目
'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?>
<li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
}
else {
echo '<li>* 暂无相干文章</li>';
}
wp_reset_query();
}
else {
echo '<li>* 暂无相干文章</li>';
}
?>
</ul>
运用说明:"不包括的分类ID" 指的是相干文章不显现该分类下的文章,将偕行的 NULL 改成文章分类的ID即可,多个ID就用半角逗号离隔。由于这里限定只显现6篇相干文章,所以不论给 query_posts() 的参数 tag__in 赋多少个值,都是只显现一个标签下的 6 篇文章,除非第一个标签有1篇,第二个标签有2篇,第三个有3篇。。。。。。所以假如这篇文章有多个标签,那末我们采用的做法是随机猎取一个标签的id,赋值给 tag__in 这个参数,猎取该标签下的6篇文章。
要领二:分类相干
本要领是经由过程猎取该文章的分类id,然后猎取该分类下的文章,来到达猎取相干文章的目标。
<ul id="cat_related">
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$args = array(
'category__in' => array( $cats[0] ),
'post__not_in' => array( $post->ID ),
'showposts' => 6,
'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?>
<li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
}
else {
echo '<li>* 暂无相干文章</li>';
}
wp_reset_query();
}
else {
echo '<li>* 暂无相干文章</li>';
}
?>
</ul>
要领三:标签相干,SQL猎取
猎取相干文章的道理与要领一类似,不过在猎取文章的时刻是以SQL语句来直接读取数据库,从而随机猎取6篇相干文章纪录,而不是WordPress的函数query_posts().
<ul id="tags_related">
<?php
global $post, $wpdb;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
$tag_list = '';
foreach ($post_tags as $tag) {
// 猎取标签列表
$tag_list .= $tag->term_id.',';
}
$tag_list = substr($tag_list, 0, strlen($tag_list)-1);
$related_posts = $wpdb->get_results("
SELECT DISTINCT ID, post_title
FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
WHERE {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
AND ID = object_id
AND taxonomy = 'post_tag'
AND post_status = 'publish'
AND post_type = 'post'
AND term_id IN (" . $tag_list . ")
AND ID != '" . $post->ID . "'
ORDER BY RAND()
LIMIT 6");
// 以上代码中的 6 为限定只猎取6篇相干文章
// 经由过程修正数字 6,可修正你想要的文章数目
if ( $related_posts ) {
foreach ($related_posts as $related_post) {
?>
<li><a href="<?php echo get_permalink($related_post->ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li>
<?php }
}
else {
echo '<li>暂无相干文章</li>';
}
}
else {
echo '<li>暂无相干文章</li>';
}
?>
</ul>
要领四:分类相干,SQL猎取
猎取相干文章的道理与要领二类似,不过在猎取文章的时刻是以SQL语句来直接读取数据库,从而随机猎取6篇相干文章纪录,而不是WordPress的函数query_posts().
<ul id="cat_related">
<?php
global $post, $wpdb;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$related = $wpdb->get_results("
SELECT post_title, ID
FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
WHERE {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id
AND {$wpdb->prefix}term_taxonomy.taxonomy = 'category'
AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
AND {$wpdb->prefix}posts.post_status = 'publish'
AND {$wpdb->prefix}posts.post_type = 'post'
AND {$wpdb->prefix}term_taxonomy.term_id = '" . $cats[0] . "'
AND {$wpdb->prefix}posts.ID != '" . $post->ID . "'
ORDER BY RAND( )
LIMIT 6");
if ( $related ) {
foreach ($related as $related_post) {
?>
<li>* <a href="<?php echo get_permalink($related_post->ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li>
<?php
}
}
else {
echo '<li>* 暂无相干文章</li>';
}
}
else {
echo '<li>* 暂无相干文章</li>';
}
?>
</ul>
要领五:作者相干
该要领是猎取该文章作者的其他文章来充任相干文章,代码以下:
<ul id="author_related">
<?php
global $post;
$post_author = get_the_author_meta( 'user_login' );
$args = array(
'author_name' => $post_author,
'post__not_in' => array($post->ID),
'showposts' => 6, // 显现相干文章数目
'orderby' => date, // 按时候排序
'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?>
<li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
}
else {
echo '<li>* 暂无相干文章</li>';
}
wp_reset_query();
?>
</ul>
时候效力对照
我们将用之前的一个php代码对以上各个相干文章代码执行时候举行测算,以便对以上各个的要领举行效力,给你的挑选供应参考。以下是在统一篇文章中猎取6篇相干文章,以上各要领终究测算的时候以下:
要领一:0.18067908287048 秒
要领二:0.057158946990967 秒
要领三:0.037126064300537 秒
要领四:0.045628070831299 秒
要领五:0.023991823196411 秒

评论列表