[wordpress主题]wordpress显示随机文章实现方法

更新时间:2018-08-09    来源:WordPress    手机版     字体:

【www.bbyears.com--WordPress】

首页随机显示文章 在wordpress里面并不难,也不需要安装复杂的插件,只需要在合适的php文件里面添加如下代码,这个完全归功于wordpress的模块化结构,代码如下

1.使用get_posts生成随机文章

 代码如下

$rand_posts = get_posts("numberposts=10&orderby=rand");
foreach( $rand_posts as $post ) :
?>
">


2.使用随处可见的query_psots

 代码如下

query_posts("showposts=10&orderby=rand");
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
">
endwhile; else:
?>
没有可显示的文章
endif;
wp_reset_query();
?>

3.自定随机文章显示

 代码如下

   
        $args = array( "numberposts" => 5, "orderby" => "rand" );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
        ">
   
   

本文来源:http://www.bbyears.com/wangyezhizuo/43474.html