wordpress网站程序虽有很多插件实现相关文章和随机文章功能,但是网站用过多的插件对网站本身是有害处的,会加重服务器的负担。
这里介绍下wordpress实现相关文章与随机文章的代码,以下便是wordpress程序相关文章与随机文章的具体代码。
wordpress随机文章具体代码:
复制代码 代码示例:
<ul>
<?php $rand_posts = get_posts('numberposts=5&orderby=rand'); foreach( $rand_posts as $post ) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
wordpress相关文章具体代码:
复制代码 代码示例:
<h3>Related Posts</h3>
<ul>
<?php
$post_num = 5; // 數量設定.
$exclude_id = $post->ID; // 單獨使用要開此行 //zww: edit
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
$tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ','
//zww: edit$args = array(
'post_status' => 'publish',
'tag__in' => explode(',', $tags), // 只選 tags 的文章. //zww: edit
'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(); ?>
<li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title
(); ?>"><?php the_title(); ?></a></li><?php
$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(); ?>
<li><a rel="bookmark" href="<?php the_permalink(); ?>">
<?php the_title(); ?></a></li><?php $i++;
} wp_reset_query();
}
if ( $i == 0 ) echo '<li>没有相关文章!</li>';
?>
</ul>
这些代码能实现相关文章功能,但它主要调取的是tag标签的相关文章,所以必须要给文章添加tag标签,如果没有tag标签或相关tag标签的文章不够,就会出现相关文章数量不够或不能显示。(www.jb200.com 脚本学堂 整理)
下面是相关文章的改进版,在tag标签的文章数量不够是会调取其他的文章作为相关文章。
相关文章的改进版具体代码:
复制代码 代码示例:
<h3>Related Posts</h3>
<ul>
<?php
$post_num = 5;
$exclude_id = $post->ID;
$posttags = get_the_tags(); $i = 0;
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(); ?>
<li><a rel="bookmark" href="<?php the_permalink(); ?>"
title="<?php the_title(); ?>"><?php the_title(); ?></a></li><?php
$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(); ?>
<li><a rel="bookmark" title="<?php the_title(); ?>"
href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php
$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(); ?>
<li><a rel="bookmark" title="<?php the_title(); ?>" href="
<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php
$exclude_id .= ',' . $post->ID; $i ++;
} wp_reset_query();
}
if ( $i == 0 ) echo '<li>No Related Articles!</li>';
?>
</ul>