热点

WordPress在网站建设开发中进行随机文章调用输出的3个方法

发布: 2024年01月05日阅读: 1,029

Wordpress 随机调用 网站建设

在平时我们用wordpress系统进行网站开发时,经常会用到随机调用数据信息的需求。这样对于我们页面或是详细页中,这样每次访问时就会展示不同的数据,每次都会更新不同的数据,这样可以给网站用户访问一种每次不同的访问变化与数据展示。

 

1、最常见的调用随机代码,这样也比较容易懂,调用数据也比较简单。

<?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 生成随机文章列表,但是这种一般只应用于 post 常规组件调用。

<?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、这个是应用于本分类随机文章数据进行调用

<?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(); ?>


 

 

 

 

推荐阅读

2021年上海松一国庆节放假安排放假通告5912021-09-29

2023年上海松一五一劳动节放假安排放假通告1,1242023-04-26

上海松一与捷捷微电股份集团官网建设项目网站建设1,0532022-04-06

WordPress 常用开发中的条件判断标签及开发用法wordpress5172024-04-09