[WordPress开发]如何调用全站、同分类随机文章

在我们的网站中会有很多地方用到随机显示文章,为了使我们的wordpress主题功能更加的强大,wordpress随机文章是wordpress主题制作中必不可少的一项功能,

以下分享2段wordpress随机文章的代码片段。

整站随机文章

//numberposts 为文章数量
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<a href="<?php the_permalink(); ?>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ><?php the_title(); ?></a>
<?php endforeach; ?>

同分类随机文章

<?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(); ?>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ><?php the_title(); ?></a>
<?php endwhile;?>
<?php wp_reset_query(); ?>

给TA打赏
共{{data.count}}人
人已打赏
投稿专区

[WordPress开发]不用插件使用七牛云存储加速网站

2020-4-23 15:52:09

投稿专区

[WordPress开发]如何调用最新、热门文章和指定分类文章

2020-4-23 15:52:12

下载说明

  • 1、绅士视界所提供的压缩包若无特别说明,解压密码均为wysacg.top
  • 2、下载后文件若为压缩包格式,请安装7Z软件或者其它压缩软件进行解压;
  • 3、部分资源后缀为 .wys 请自行修改为 .7z
  • 4、资源可能会由于内容问题被和谐,遇到此问题,请到文章页面进行反馈,以便无言及时进行更新;

站长声明

本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有;若为付费资源,请在下载后24小时之内自觉删除;若作商业用途,请到原网站购买;由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索