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

相关推荐

  • 如何做好页面标题标签内容优化编辑-Title Tags SEO优化全面指南(内附谷歌官方标题优化文档)

    想要提高网站页面的SEO优化,页面的标题标签优化是非常重要的一环,如果你会把页面的标题标题标签(Title Tag)优化做好,那么你的网站有显著的自然排名提升。 标题优化的好处如下: 1. 提升网站整体SEO质量; 2. 增加内容营销效果; 3. 提升搜索引擎排名; 4. 提高用户点击率CTR(Click-through Rate) A. 什么是标题标签? …

    google seo 2025年4月23日
    1.8K00
  • wordpress和shopify的详细区别

    wordPress 和 Shopify 是目前最流行的两大建站平台之一,尤其适用于电商、内容型网站或个人博客。它们各有优势,适合不同需求。 下面我们从功能、适用人群、价格、扩展性、操作难度等方面,来详细比较一下两者的区别。 一、基本定义 🧩 WordPress 👉 重点讨论 WordPress.org,是大多数建站者首选。 🛒 Shopify 二、核心定位区…

    外贸 2025年4月8日
    1.2K00
  • 常用的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日
    82700
  • wordpress开发建设常用调用代码

    1、获取页面分类列表 <?php query_posts(‘showposts=6&cat=54’); //cat=1为调用ID为1的分类下文章 while(have_posts()) : the_post(); ?> <li> <a href=”<?php echo the_permalink() ?>” …

    外贸 2025年3月22日
    1.2K00
  • wordpress常用标签大全

    WordPress Header头部 PHP代码 <?php bloginfo(‘name’); ?> 网站标题 <?php wp_title(); ?> 日志或页面标题 <?php bloginfo(‘stylesheet_url’); ?> WordPress主题样式表文件style.cs…

    外贸 2025年4月19日
    1.1K00

发表回复

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

联系我们

在线咨询: QQ交谈

邮件:156738051@qq.com

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

关注微信