AT互联|WordPress如何调用全站、同分类随机文章_
AT互联|WordPress如何调用全站、同分类随机文章_手机扫码预览

AT互联|WordPress如何调用全站、同分类随机文章_

330
免费 优惠信息:免费 暂无永久SVIP下载特权
  • 免费售前咨询
  • 登录下载更划算哦
  • 付费安装资源
  • 付费终身升级
  • QQ保障售后服务
  • 网站应急咨询顾问

在我们的网站中会有很多地方用到随机显示文章,为了使我们的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(); ?>

 

文章来自AT互联https://www.at8848.cn转载请注明出处,并保留本链接,谢谢!

打破行业潜规则-更多选择 更低价格 更快交付!
AT互联全栈开发服务商 » AT互联|WordPress如何调用全站、同分类随机文章_
欢迎您光顾,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡