我有一个循环,应该显示最近的博客文章。问题是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>
这是正确的循环方式吗? 我遇到的问题是,现在我看不到任何博客文章。
答案 0 :(得分:0)
根据我在代码中看到的内容,您正在while
循环之外显示内容。另外,您正在将查询参数数组与$wp_query->query
合并,但它们是不同的。您要使用:$wp_query->query_vars
在继续阅读之前,请确保您访问以下链接:
您的代码应类似于:
<?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>