简体   繁体   中英

wp_query - limit number od posts, and pages

OK, I creating all posts page view, and stuck on loop. Here is the wp_query:

  <?php if ( ! is_single() ) : ?>
<div class="entry-content">
    <?php

    $first_articles = new WP_Query( 
                    array(
                        'posts_per_page' => 2,
                        'numberposts' => 2,
                        'category_name' => 'artykuly',
                        'order'   => 'DESC',
                    ) 
                );

    wp_link_pages( array(
        'before'      => '<div class="page-links">' . __( 'Pages:', 'twentyseventeen' ),
        'after'       => '</div>',
        'link_before' => '<span class="page-number">',
        'link_after'  => '</span>',
    ) );
    ?>

    <?php if ( $first_articles->have_posts() ) : while ( $first_articles->have_posts() ) : $first_articles->the_post(); ?>
    <div class="kat-img" style="background: url('<?php the_post_thumbnail_url(); ?>')">
        <div><?php the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' ); ?></div>
        <div><?php echo excerpt(10); ?></div>
        <div><?php echo get_the_date(); ?></div>
        <div><?php echo the_tags( '<ul><li>', '</li><li>', '</li></ul>' ); ?></div>
    </div>
    <?php endwhile; endif; wp_reset_query(); ?> 

</div><!-- .entry-content -->
<?php endif; ?>

This is the code inside content.php (there is nothing more, only this) and problem with this loop is that it display 2 latest post but these 2 latest post loop is cloned and displayed 6 times. Where is the problem? Should I add some more limits for this loop? I want to display 2 latest posts only one time.

There is most probably another loop in the outer file like archive.php which is being used to display posts.

Either add this code directly in archive.php or category.php or remove the other loop from those files.

Also remove 'numberposts' => 2 from the wp query, it is not needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM