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

相关推荐

  • WordPress 时间日期函数

    我们用wordpress系统进行网站建设开发网站时,关于wordpress的时间相关函数应用,以及wp的时间显示函数。 1、输出 2024-12-25 <?phpthe_time(‘Y-m-d’);?> 2、输出时间,如:10:35:28 <?phpthe_time(‘G:i:s’);?> 3、输出 2024年12月25日 <?…

    外贸 2025年3月22日
    1.3K00
  • 如何做好页面标题标签内容优化编辑-Title Tags SEO优化全面指南(内附谷歌官方标题优化文档)

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

    google seo 2025年4月23日
    1.9K00
  • 美国苹果账号(Apple ID)注册方法

    准备工作: 1、提前注册一个国内的Apple ID(这个无需多说) 2、准备电脑一台,或者手机一部 开始注册美国Apple ID: 1、先进入苹果Apple ID官网https://appleid.apple.com/#!&page=signin,然后选择“登录”! 2、输入你提前准备好了的国内的Apple ID,输入账号和密码,并登录!(不建议使用…

    2025年6月1日
    1.8K00
  • RankMath SEO插件导致TAG页面无法收录的解决办法

    1、GSC报错 2、看了受影响的页面,果然! 3、再去看RankMathSEO插件生成的sitemap,有post,page,category,但是也没有tag,那就是插件本身并没有开启收录tag页面。 RankMath SEO插件解决tag页面正常收录的解决办法1、RankMath SEO-post-tag-tag archives robots meta…

    2025年4月13日
    1.3K00
  • 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.3K00

发表回复

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

联系我们

在线咨询: QQ交谈

邮件:156738051@qq.com

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

关注微信