在wordpress调用随机文章的方法其实有很多种方法可以实现,这里分享一种比较简单的,以下是相关的代码片段。
<ul><h2>随机文章</h2><?php
$count = 1;
if (is_home()) {
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
if ($paged == '1') {
$offset = '0';
}else{
$offset = ($paged - 1) * get_option('posts_per_page') + 0;
}
$arry = array(
'paged' => $paged,
'offset' => $offset,
'ignore_sticky_posts' => 1
);
query_posts($arry);
}
?><?php $i=1; $rand_posts = get_posts('numberposts=13&orderby=rand');
foreach( $rand_posts as $post ) : ?><li><a title="<?php the_title();?>" href="<?php the_permalink();?>" itemprop="url" ><?php the_title();?></a></li><?php $i+=1; endforeach; wp_reset_query(); ?></ul>
如果想要在自己的wordpress主题模板中使用以上随机文章代码,只需要将该代码插入想要现实的模板文件位置就可以了,比如index.php(首页)文件中,然后在使用css进行相应的样式美化一下就可以了。