posts_per_page показывает только 1 пост вместо 3 постов - PullRequest
1 голос
/ 10 июля 2019

У меня есть цикл, который должен показывать последние сообщения в блоге. Проблема в том, что posts_per_page показывает только один пост вместо трех постов. Я не мог понять, где я иду не так.

Я попробовал следующие шаги, но не помогло, но у меня не получилось.

ignore_sticky_posts = > true, 
update_post_term_cache=>false, 
nopaging=>true 

Код:

<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>

Это правильный способ зацикливания? Проблема, которую я получаю, состоит в том, что теперь я не вижу ни одного сообщения в блоге.

1 Ответ

0 голосов
/ 10 июля 2019

Из того, что я вижу в вашем коде, вы отображаете свой контент вне цикла while.Кроме того, вы объединяли массив аргументов запроса с $wp_query->query, и они отличаются.Вы хотите использовать: $wp_query->query_vars

Прежде чем читать дальше, обязательно перейдите по следующим ссылкам:

query_posts () документация

Документация Loop

Ваш код должен выглядеть примерно так:

<?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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...