下吧资源网_专注精品网站源码下载的网站_免费商业源码分享

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(); ?>

 

本文链接:https://www.xiaba.cc/html/2104.html

版权声明:站内所有文章皆来自网络转载,只供模板演示使用,并无任何其它意义!

发表评论 共有 0 条评论)

联系客服
网站客服 业务合作 Q交流群
123456789
公众号
公众号
公众号
返回顶部