我在网格中显示帖子(4个帖子宽2个帖子高)。我在用一张桌子。为了让内容显示在第二行上,我使用了两个wp\\u查询(每行一个),因为我不知道如何在循环中生成第二行。
我想知道我是否可以用一个查询来实现相同的布局,而不必做两个查询。我怎样才能做到这一点?
下面是我的代码中我也提到的部分:
<table>
<tr>
<?php
$args = array (
posts_per_page => 4,
post_type => \'post\',
orderby => \'meta_value_number\',
meta_key => \'episode\',
order => \'ASC\',
post_status => array(\'publish, future\')
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$pID = $post->ID ?>
<td>
<form id="episode-<?php echo $pID; ?>" name="episode-<?php echo $pID; ?>" method="post" action="">
<div class="gallery-thumb">
<a onclick="document.getElementById(\'episode-<?php echo $pID; ?>\').submit();"><?php the_post_thumbnail(); ?></a>
</div>
<input type="hidden" name="pID" value="<?php echo $pID; ?>" />
</form>
<div class="gallery-text">
Episode <?php the_field(\'episode\'); ?><br />
"<?php the_title(); ?>"<br />
<?php
if (get_post_status() == \'publish\') {
echo get_the_time(\'M d, Y\');
} else {
echo \'Premieres \'; echo get_the_time(\'M d\');
}
?>
</div>
</td>
<? }
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
</tr>
<tr>
<?php
$args = array (
posts_per_page => 4,
offset => 4,
post_type => \'post\',
orderby => \'meta_value_number\',
meta_key => \'episode\',
order => \'ASC\',
post_status => array(\'publish, future\')
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$pID = $post->ID?>
<td>
<form id="episode-<?php echo $pID; ?>" name="episode-<?php echo $pID; ?>" method="post" action="">
<div class="gallery-thumb">
<a onclick="document.getElementById(\'episode-<?php echo $pID; ?>\').submit();"><?php the_post_thumbnail(); ?></a>
</div>
<input type="hidden" name="pID" value="<?php echo $pID; ?>" />
</form>
<div class="gallery-text">
Episode <?php the_field(\'episode\'); ?><br />
"<?php the_title(); ?>"<br />
<?php
if (get_post_status() == \'publish\') {
echo get_the_time(\'M d, Y\');
} else {
echo \'Premieres \'; echo get_the_time(\'M d\');
}
?>
</div>
</td>
<? }
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
</tr>
</table>
最合适的回答,由SO网友:vancoder 整理而成
在一个查询中获取所有帖子,然后计算循环的迭代次数。
下面是伪代码。
if ( $query->have_posts() ) {
$i = 0;
while ( $query->have_posts() ) {
// If $i is 5 (5th post), end one row and start the next
if (++$i == 5) {
echo \'</tr><tr>\';
}
// the rest of your loop output