繁体   English   中英

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

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

好的,我创建所有帖子页面视图,并坚持循环。 这是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; ?>

这是content.php中的代码(仅此而已),此循环的问题是它显示2个最新发布,但是这2个最新发布循环被克隆并显示6次。 问题出在哪儿? 我是否应该为此循环添加更多限制? 我想只显示一次2个最新帖子。

外部文件中很可能还有另一个循环,例如archive.php,它用于显示帖子。

可以直接在archive.php或category.php中添加此代码,也可以从这些文件中删除其他循环。

同样从wp查询中删除'numberposts' => 2 ,不需要它。

暂无
暂无

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

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