如果你仅仅是实现随机文章功能,建议你阅读给WordPress后台小工具增加一个随机文章
我们首先建立一个PHP文件放到includes文件夹内,命名随便,比如random.php,然后在想要调用的页面添加调用代码:
<?php include(‘includes/random.php’);?>
以下是random.php文件的全部代码:
<div id=”related-posts”>
<div class=”caption”>带缩略图的相关文章:</div>
<div class=”related” class=”clearfix”>
<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), // 只选 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 href=”<?php echo the_permalink(); ?>” title=”<?php the_title(); ?>” ><img src=”<?php if ( get_post_meta($post->ID, ‘thumbnail1’, true) ) { echo get_post_meta($post->ID, ‘thumbnail1’, true);}
elseif ( has_post_thumbnail() ){$thumbnail1_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ‘thumbnail1’);echo $thumbnail1_url[0];}else{echo catch_first_image();} ?>” alt=”<?php the_title(); ?>” width=”120″ height=”120″ ></a>
<a href=”<?php echo the_permalink(); ?>” ><?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 href=”<?php echo the_permalink(); ?>” title=”<?php the_title(); ?>” ><img src=”<?php if ( get_post_meta($post->ID, ‘thumbnail1’, true) ) { echo get_post_meta($post->ID, ‘thumbnail1’, true);}
elseif ( has_post_thumbnail() ){$thumbnail1_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ‘thumbnail1’);echo $thumbnail1_url[0];}else{echo catch_first_image();} ?>” alt=”<?php the_title(); ?>” width=”120″ height=”120″ ></a>//缩略图大小设定
<a href=”<?php echo the_permalink(); ?>” ><?php the_title(); ?></a></li>
<?php
$i++;
} wp_reset_query();
}
if ( $i == 0 ) echo ‘<li>没有相关文章!</li>’;
?>
</ul>
</div>
</div>
<!– release post end –>
当然要根据你网页的风格修改随机文章的样式,以下仅供参考。
/** related post thumbnail **/
#related-posts{border-radius:5px;border-top:3px #dfdfdf solid;background:#fff;clear:both;}
.caption{color:#0C0C0B;font-size: 14px;font-weight:bold;padding-top: 10px;margin-bottom: 10px;margin-left:10px;}
.related {white-space:normal;margin:0 auto;overflow: hidden;border-left: 1px solid #DFDFDF;margin-left:20px;}
.related li{float: left;width: 120px;height:180px;list-style:none;border-right:1px solid #dfdfdf;padding:4px;_padding:2px;text-align: left;}
.related a{float: left;width: 126px;text-overflow: ellipsis;-o-text-overflow: ellipsis;}
.related a:hover{white-space: normal;}
.related li:hover{background:#F6F6F6;}
.related img{height:120px;width:120px;border:0;padding:0;}