WordPress如何调用全站、同分类随机文章

2020-07-02 0 80

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

<?php endwhile;?>
<?php wp_reset_query(); ?>

 

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

「陌客网」 WordPress如何调用全站、同分类随机文章 https://www.moke1.cn/843.html

常见问题

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务