- 资源介绍
- 更新记录
- 安装教程
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(); ?>
猜你喜欢
-
WordPress如何在父分类获取所有子分类/二级分类
2021-04-25 -
【WP导航主题】黑格超大气简约网址导航网站模板
2021-04-21 -
WordPress教程:在后台文章列表添加作者筛选选项
2021-04-25 -
wp_get_theme()函数
2021-04-26 -
WordPress网页设计中常见的六种布局方式
2021-04-26 -
get_term_by()函数
2021-04-26 -
is_post_type_archive()函数
2021-04-26 -
WordPress如何调用指定ID文章
2021-04-25 -
delete_post_meta()函数
2021-04-26 -
WP Rocket – 汉化版WP超强缓存插件已更新至V 3.5.4
2021-04-24
-
RiPro小八子子主题最新v8.0版 虚拟资源交易
2021-04-24 -
focus-start群主自费300购买的企业主题
2021-04-24 -
智能新闻报道积分商场主题风格LensNews全新V3.0去受权无限制版本
2021-04-21 -
WordPress 5.5 如何自定义XML 站点地图功能
2021-04-26 -
在线工具箱V2.0 带后台源码(100+工具)
2021-04-25 -
RiPro子主题/小八子主题V1.5.5版【优惠码折扣+工单系统+任务系统+实名认证+自助广告位购买+多模块布局】
2021-04-24 -
RIPRO美化-DIY分类页分页导航
2021-04-25 -
5个常见的WordPress错误以及解决方法
2021-04-25 -
【Typecho主题】handsome6.0主题去授权
2021-04-21 -
RIPRO主题美化-网站二级导航菜单统计文章数量
2021-04-24
猜你在找
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 大资源
- 2021-04-25Hi,初次和大家见面了,请多关照!
最后编辑:2021-04-25

