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

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

主题测试文章,只做测试使用。发布者:zhaolou,转转请注明出处:https://www.zhaolou.cc/archives/4229

赞 (0)
zhaolou's avatarzhaolou
上一篇 2025年3月22日 上午10:14
下一篇 2025年3月22日 上午10:16

相关推荐

  • 2025 最新对比:Blocksy Pro、Kadence、Astra 功能+价格+性能全解析!

    总体结论: Blocksy Pro = 视觉表现最强 + 功能集最多 + 动效细腻Kadence = 轻便易用 + 电商友好 + 性能均衡Astra = 性能最优 + 生态强大 + 适合SEO和内容站 01. 功能模块对比功能模块Blocksy ProKadenceAstraHeader/Footer Builder✅ 拖拽+动态显示逻辑✅ 拖拽+条件逻辑支…

    外贸 2025年7月6日
    3.5K00
  • 如何用 Semrush + Google Trends + Ahrefs 高效挖掘关键词?

    作为Google SEO中最核心的一步,关键词挖掘不仅关系到流量多少,还决定了你内容能否打入目标用户搜索视野。本文以“xxx”为例,带你实操一遍关键词挖掘与筛选的完整流程。 ✅ Step 1:用 Semrush 查询关键词基础数据(Keyword Overview) 打开 Semrush,在左侧点击【Keyword Overview】,输入你想查询的关键词,…

    google seo 2025年5月14日
    4.5K00
  • 外贸网站内链SEO优化对搜索引擎排名影响及注意事项详解

    一. 什么是内部链接? 内部链接是同一域名下页面与页面之间的链接,客户通过点击锚文本链接进入到其他的页面,外部链接指的是其他域名指向本域名的链接。 二. 为什么内部链接对于SEO优化非常重要?   内部链接可以帮助谷歌查找,收录并理解你网站所有页面。 如果你有策略地布局内链,那么会让你的页面有效地传递权重(相当于提升页面权威度)…

    google seo 2025年4月23日
    2.1K00
  • 常用的Rank Math SEO 中英文对照

    Basic SEO Add Focus Keyword to the SEO title.Add Focus Keyword to your SEO Meta Description.Use Focus Keyword in the URL.Use Focus Keyword at the beginning of your content.Use Focu…

    google seo 2025年9月10日
    2.2K00
  • wordpress 进行文章列表分页

    把以下代码放入:functions.php function lingfeng_pagenavi( $range = 4 ) { global $paged,$wp_query; if ( !$max_page ) { $max_page = $wp_query->max_num_pages; } if( $max_page >1 ) { ech…

    外贸 2025年3月22日
    1.8K00

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

在线咨询: QQ交谈

邮件:156738051@qq.com

工作时间:周一至周五,9:00-18:00,节假日休息

关注微信