欢迎光临
我们一直在努力

wordpress如何调用随机文章和随机推荐文章

wordpress怎样调用随机文章

方法一:foreach循环

<?php
$rand_posts = get_posts('numberposts=10&orderby=rand');
foreach( $rand_posts as $post ) :
?>
<!–下面是你想自定义的Loop–>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>

方法二:

<?php
$query = array(
'post_type' => 'post',
'orderby' => 'rand'
);
$posts = new WP_Query( $query );
if ( $posts->have_posts() ) {
while( $posts->have_posts() ) :
$posts->the_post();
the_content();
endwhile;
}
wp_reset_query();
?>

wordpress随机调用置顶推荐的文章代码:

<?php
//获取置顶文章的ID串
$rand_id = get_option( 'sticky_posts' );
$query = array(
'post__in' => $rand_id,
'post_type' => 'post',
'orderyby' => 'rand',
'numberposts' => 2
);
$posts = new WP_Query( $query );
if ( $posts->have_posts() ) {
while( $posts->have_posts() ) :
$posts->the_post();
the_content();
endwhile;
}
wp_reset_query();
?>

 

赞(0) 打赏
未经允许不得转载:新起点博客 » wordpress如何调用随机文章和随机推荐文章


关注公众号『新起点软件管家』

获取最新网络资源及破解软件!
带你玩转各样软件...

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏