在平時(shí)我們用wordpress系統(tǒng)進(jìn)行網(wǎng)站開發(fā)時(shí),經(jīng)常會(huì)用到隨機(jī)調(diào)用數(shù)據(jù)信息的需求。這樣對(duì)于我們頁面或是詳細(xì)頁中,這樣每次訪問時(shí)就會(huì)展示不同的數(shù)據(jù),每次都會(huì)更新不同的數(shù)據(jù),這樣可以給網(wǎng)站用戶訪問一種每次不同的訪問變化與數(shù)據(jù)展示。
1、最常見的調(diào)用隨機(jī)代碼,這樣也比較容易懂,調(diào)用數(shù)據(jù)也比較簡單。
<?php $args?=?array(?'events'?=> 5,?'orderby'?=>?'rand',?'post_status'?=>?'publish'?); $rand_posts?= get_posts(?$args?); foreach(?$rand_posts?as?$post?) : ?> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> <?php?endforeach; ?>
2、也可以采用 query_posts 生成隨機(jī)文章列表,但是這種一般只應(yīng)用于 post 常規(guī)組件調(diào)用。
<?php $args?=?array(?'events'?=> 5,?'orderby'?=>?'rand',?'post_status'?=>?'publish'?); $rand_posts?= get_posts(?$args?); foreach(?$rand_posts?as?$post?) : ?> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> <?php?endforeach; ?>
3、這個(gè)是應(yīng)用于本分類隨機(jī)文章數(shù)據(jù)進(jìn)行調(diào)用
<?php
$cat = get_the_category();
foreach($cat as $key=>$category){
$catid = $category->term_id;
}
$args = array('orderby' => 'rand','showposts' => 8,'cat' => $catid );
$query_posts = new WP_Query();
$query_posts->query($args);
while ($query_posts->have_posts()) : $query_posts->the_post();
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile;?>
<?php wp_reset_query(); ?>