我正在尝试显示最近的帖子,左侧显示图片、标题、摘录和作者信息,右侧仅显示标题、日期和作者信息。我希望它在右侧列中显示剩余信息(彼此堆叠)。到目前为止,我在左边有一篇最近发表的文章,在左边一栏有下一篇文章的标题,但第三篇文章并没有停留在右边一栏,而是在最近发表的文章#1下向左移动。我拥有的:
我正在努力实现的目标:
这是我的代码:
<div class="row">
<?php
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => 3
);
$counter = 0;
$_posts = new WP_Query($args);
?>
<?php if( $_posts->have_posts() ) : ?>
<?php while ( $_posts->have_posts() ) : $_posts->the_post(); $counter++ ?>
<?php if ( $counter === 0 || $counter === 1) : ?>
<div class="col-md-6 float-left">
<div>
<a href="<?php the_permalink(); ?>" class="mb-4 d-block"><?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( \'full\' );
}
?></a>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div>
</div>
<?php else : ?>
<div class="col-md-6 float-right">
<div class="post-entry mb-4">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="text-muted mb-3 text-uppercase small"><span><?php the_time(\'F jS, Y\'); ?></span> by <a href="<?php the_permalink(); ?>" class="by"><?php the_author( \', \' ); ?></a></p>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
任何帮助都将不胜感激。非常感谢。
最合适的回答,由SO网友:Orbital 整理而成
Try this
<div class="row">
<?php
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => 3
);
$counter = 0;
$_posts = new WP_Query($args);
?>
<?php if( $_posts->have_posts() ) : ?>
<?php while ( $_posts->have_posts() ) : $_posts->the_post(); $counter++ ?>
<?php if ( $counter === 0 || $counter === 1) : ?>
<div class="col-md-6 float-left">
<div>
<a href="<?php the_permalink(); ?>" class="mb-4 d-block"><?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( \'full\' );
}
?></a>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div>
</div>
<div class="col-md-6 float-right">
<?php else : ?>
<div class="post-entry mb-4">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="text-muted mb-3 text-uppercase small"><span><?php the_time(\'F jS, Y\'); ?></span> by <a href="<?php the_permalink(); ?>" class="by"><?php the_author( \', \' ); ?></a></p>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
</div>
SO网友:Gopala krishnan
希望这段代码能对你有所帮助,这段代码将以之字形打印输出(即,一篇文章剩余,下一篇文章剩余)等等。
<div class="row"><?php
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => 3
);
$counter = 1;
$_posts = new WP_Query($args);
if( $_posts->have_posts() ) :
while ( $_posts->have_posts() ) : $_posts->the_post();
if ( $counter%2 !=0) : ?>
<div class="col-md-6 float-left">
<div>
<a href="<?php the_permalink(); ?>" class="mb-4 d-block"><?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( \'full\' );
}
?></a>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div>
</div><?php
else : ?>
<div class="col-md-6 float-right">
<div class="post-entry mb-4">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="text-muted mb-3 text-uppercase small"><span><?php the_time(\'F jS, Y\'); ?></span> by <a href="<?php the_permalink(); ?>" class="by"><?php the_author( \', \' ); ?></a></p>
</div>
</div><?php
endif;
$counter++;
endwhile;
endif;
wp_reset_postdata(); ?></div>