WordPress主题制作之相关文章代码

发布日期:

查找相同分类,随机输出文章

<?php
$categories = get_the_category($post->ID);
if ($categories) {
    $category_ids = array();

    foreach ($categories as $individual_category) {
        $category_ids[] = $individual_category->term_id;
    }

    $args = array(
        'category__in' => $category_ids,
        'post__not_in' => array($post->ID),
        'ignore_sticky_posts' => 1,
        'showposts' => 6,
        'orderby' => 'rand',
    );

    $my_query = new wp_query($args);
    if ($my_query->have_posts()) {
        while ($my_query->have_posts()) {
            $my_query->the_post();
            ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
        }

    }
    wp_reset_postdata();
}
?>