首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只显示1个帖子而不是3个帖子的posts_per_page

只显示1个帖子而不是3个帖子的posts_per_page
EN

Stack Overflow用户
提问于 2019-07-11 00:33:33
回答 1查看 1.2K关注 0票数 1

我有一个循环,应该可以显示最近的博客文章。问题是posts_per_page只显示一个帖子,而不是三个帖子。我不知道我错在哪里。

我尝试了以下步骤,但没有帮助,但对我不起作用。

代码语言:javascript
复制
ignore_sticky_posts = > true, 
update_post_term_cache=>false, 
nopaging=>true 

代码是:

代码语言:javascript
复制
<section class="ftco-section" id="blog-section">
  <div class="container">
    <div class="row justify-content-center mb-5 pb-5">
      <div class="col-md-7 heading-section text-center ftco-animate">
        <h1 class="big big-2">Blog</h1>
        <h2 class="mb-4">Our Blog</h2>
        <p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia</p>
      </div>
    </div>
    <div class="row d-flex">
    <?php if (have_posts()) :
    while (have_posts()) :the_post(); ?>
      <div class="col-md-4 d-flex ftco-animate">
        <div class="blog-entry justify-content-end">
          <a href="<?php the_permalink(); ?>" class="block-20" style="background-image: url('<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>');">
          </a>
          <div class="text mt-3 float-right d-block">
            <div class="d-flex align-items-center mb-3 meta">
                <p class="mb-0">
                    <span class="mr-2">June 21, 2019</span>
                    <a href="#" class="mr-2">Admin</a>
                    <a href="#" class="meta-chat"><span class="icon-chat"></span> 3</a>
                </p>
            </div>
            <h3 class="heading"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <p><?php echo wp_trim_words(get_the_excerpt(), 30); ?></p>
          </div>
        </div>
      </div>
     <?php endwhile; ?>
     <?php endif; ?>
    </div>
  </div>
</section>

这是正确的循环方式吗?我得到的问题是,现在我看不到任何博客帖子。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-11 03:19:14

根据我在代码中看到的,您在while循环之外显示了您的内容。此外,您将查询参数数组与$wp_query->query合并在一起,它们是不同的。您想要使用:$wp_query->query_vars

在进一步阅读之前,请确保访问以下链接:

query_posts() documentation

The Loop documentation

你的代码应该是这样的:

代码语言:javascript
复制
<?php
global $wpdb;

$args = array(
  'post_type' => 'post',
  'orderby' => 'date',
  'order' => 'DESC',
  'posts_per_page' => 3,
);

$this_query = new WP_Query( $args );

?>

<section class="ftco-section" id="blog-section">
  <div class="container">
    <div class="row justify-content-center mb-5 pb-5">
      <div class="col-md-7 heading-section text-center ftco-animate">
        <h1 class="big big-2">Blog</h1>
        <h2 class="mb-4">Our Blog</h2>
        <p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia</p>
      </div>
    </div>
    <div class="row d-flex">
      <?php if ( $this_query->have_posts() ) : ?>
        <?php while ( $this_query->have_posts() ) : $this_query->the_post(); ?>
          <div class="col-md-4 d-flex ftco-animate">
            <div class="blog-entry justify-content-end">
              <a href="<?php the_permalink(); ?>" class="block-20" style="background-image: url('<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>');">
              </a>
              <div class="text mt-3 float-right d-block">
                <div class="d-flex align-items-center mb-3 meta">
                    <p class="mb-0">
                        <span class="mr-2">June 21, 2019</span>
                        <a href="#" class="mr-2">Admin</a>
                        <a href="#" class="meta-chat"><span class="icon-chat"></span> 3</a>
                    </p>
                </div>
                <h3 class="heading"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <p><?php echo wp_trim_words(get_the_excerpt(), 30); ?></p>
              </div>
            </div>
          </div>
        <?php endwhile; 
        wp_reset_postdata();
      else : ?>
        <p><?php esc_html_e( 'Sorry, no posts found.' ); ?></p>
      <?php endif; ?>
    </div>
  </div>
</section>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56975053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档