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

 

1. 本站所有资源来源于用户上传和网络,如有侵权请联系右侧客服删除!
2. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站不保证所提供下载的资源的准确性、安全性和完整性,源码仅供下载学习之用!
8. 如用于商业或者非法用途,与本站无关,一切后果请用户自负!
9. 如遇到加密压缩包,默认解压密码为"aawp.cn",如遇到无法解压的请联系管理员!
当动演示 » WordPress如何调用全站、同分类随机文章