<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 7,
'paged' => $paged );
$wp_query = new WP_Query($args);
$count = 0;
while ( have_posts() ) : the_post(); ?>
<?php echo get_the_title(); ?>
<?php
$count++;
if ($count === 1) { ?>
<div class="column small-12 margin-bottom-small">
<div class="panel-brown">
<img src="" alt="" />
<h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5>
<div class="para-white">
<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?>
</div>
</div>
</div>
<?php }
while ($count <= 2) {
$count++ ?>
<div class="column small-12 medium-6 margin-bottom-small">
<div class="panel-tan">
<h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5>
<div>
<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?>
</div>
</div>
</div>
<?php }
while ($count <= 4) {
$count++ ?>
<div class="column small-12 medium-6 margin-bottom-small">
<div class="panel-tan">
<h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5>
<div>
<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?>
</div>
</div>
</div>
<?php }
?>
<?php endwhile; ?>
<!-- Pagination links -->
<?php next_posts_link( '← Older posts', $wp_query ->max_num_pages); ?>
<?php previous_posts_link( 'Newer posts →' ); ?>
我很难理解为什么if和while语句循环相同的帖子,但直接在这行之后:
while ( have_posts()) : the_post();
在我回复标题的地方,这里显示的是帖子列表。
我想在这里实现的是为每个在循环中输出的帖子创建不同的html布局。
欢呼
我认为您正在尝试循环您的查询结果,如WP_Query官方文档中所示。
另外,不知道为什么你会做while循环,就像你在主post循环内部一样,所以我把它们改成了if语句。
我相信这就是你想要的:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 7,
'paged' => $paged );
$wp_query = new WP_Query($args);
$count = 0;
if(! $wp_query->have_posts())
die('no posts!'); // ADDED - Handle this more elegantly
while ( $wp_query->have_posts() ) : $wp_query->the_post(); // ADDED - Use query result object?>
<?php echo get_the_title(); ?>
<?php
$count++;
if ($count === 1) { ?>
<div class="column small-12 margin-bottom-small">
<div class="panel-brown">
<img src="" alt="" />
<h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5>
<div class="para-white">
<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?>
</div>
</div>
</div>
<?php }
if ($count <= 2) {
?>
<div class="column small-12 medium-6 margin-bottom-small">
<div class="panel-tan">
<h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5>
<div>
<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?>
</div>
</div>
</div>
<?php }
if ($count <= 4) { // ADDED - Be careful, this will fire as well as the if statement above it since they are both <= 2
?>
<div class="column small-12 medium-6 margin-bottom-small">
<div class="panel-tan">
<h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5>
<div>
<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?>
</div>
</div>
</div>
<?php }
?>
<?php endwhile; ?>
<!-- Pagination links -->
<?php next_posts_link( '← Older posts', $wp_query ->max_num_pages); ?>
<?php previous_posts_link( 'Newer posts →' ); ?>
这不起作用的原因是我在while循环的多个地方调用了count++,这意味着它永远不会在正确的计数上。我通过把它放在if和while循环之外来修复这个问题。
谢谢大家的努力