Wordpdress调用指定分类目录文章随机文章代码示例:
<?php
// 设置分类目录参数
$cat_id = 12; // 指定分类目录的ID
$args = array(
'numberposts' => 20, // 要获取的随机文章数量
'orderby' => 'rand', // 随机排序
'cat' => $cat_id // 指定分类目录的ID
);
// 获取随机文章
$random_posts = get_posts($args);
// 输出随机文章
if ($random_posts) {
foreach ($random_posts as $post) {
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a><li>
<?php
}
wp_reset_postdata(); // 重置$post对象
} else {
echo '没有找到随机文章!';
}
?>


