简体   繁体   English

wp_query - 限制数量od帖子和页面

[英]wp_query - limit number od posts, and pages

OK, I creating all posts page view, and stuck on loop. 好的,我创建所有帖子页面视图,并坚持循环。 Here is the wp_query: 这是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. 这是content.php中的代码(仅此而已),此循环的问题是它显示2个最新发布,但是这2个最新发布循环被克隆并显示6次。 Where is the problem? 问题出在哪儿? Should I add some more limits for this loop? 我是否应该为此循环添加更多限制? I want to display 2 latest posts only one time. 我想只显示一次2个最新帖子。

There is most probably another loop in the outer file like archive.php which is being used to display posts. 外部文件中很可能还有另一个循环,例如archive.php,它用于显示帖子。

Either add this code directly in archive.php or category.php or remove the other loop from those files. 可以直接在archive.php或category.php中添加此代码,也可以从这些文件中删除其他循环。

Also remove 'numberposts' => 2 from the wp query, it is not needed. 同样从wp查询中删除'numberposts' => 2 ,不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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