我有3个类别,我想在一个循环上显示每个类别的最后3

时间:2017-10-02 作者:nnikkos

我有3个类别,我想在循环中显示每个类别的最后3篇文章,或者一个类别的最后4篇文章和其他类别的最后2篇文章,等等

我找不到任何解决办法,我尝试了很多事情,但都没有结果。你能帮帮我吗?

1 个回复
SO网友:Marcelo Henriques Cortez

If you dont have the same post with more than 1 category, you can do:

$args = array(
    \'cat\' => $category->term_id,
    \'post_type\' => \'post\',
    \'posts_per_page\' => \'3\',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
    <section class="<?php echo $category->name; ?> listing">
        <h2>Latest in <?php echo $category->name; ?>:</h2>
        <?php while ( $query->have_posts() ) {
            $query->the_post();
            ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class( 
\'category-listing\' ); ?>>
            <?php if ( has_post_thumbnail() ) { ?>
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail( \'thumbnail\' ); ?>
                </a>
            <?php } ?>

            <h3 class="entry-title">
                <a href="<?php the_permalink(); ?>">
                    <?php the_title(); ?>
                </a>
            </h3>

            <?php the_excerpt( __( \'Continue Reading <span class="meta-nav">&rarr;</span>\', \'twentyfourteen\' ) ); ?>

        </article>

    <?php } // end while ?>

</section>

<?php } // end if

// Use reset to restore original query.
wp_reset_postdata();
结束

相关推荐

Show the excerpt in a loop

我想用这个循环显示最后3篇文章。这很有效,但我不知道为什么,但摘录总是一样的。我做错了什么? <?php $args = array( \'numberposts\' => \'3\' ); $recent_posts = wp_get_recent_posts( $args ); foreach( $recent_posts as $recent ){ ?> <div class=\"