Wordpdress调用指定分类目录文章随机文章代码示例

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 '没有找到随机文章!';
}
?>
    A+
所属分类:WordPress
发布日期:2024年09月15日 16:23:37
最后更新时间:2024-09-15 16:23:37
付杰

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: