【发布时间】:2015-08-28 06:12:38
【问题描述】:
我正在尝试在 PHP 循环中插入 2 个静态 div,特别是一个在循环的最开头和一个在最后。
这 2 个 div 必须出现在它们对应的 .row 父级中,该父级当前环绕每 3 个 DIV。我该怎么做?
编辑 这是描述我需要的图像,粉红色块是手动插入的 div,与蓝色 div 具有不同的内容。那些蓝色的 div 只是 WP 帖子:
这是我的 PHP,目前这会在第一行和最后一行中创建 4 列,它应该只是 3 列:
<?php static $c=1;
$subs = new WP_Query( array( 'post_parent' => 14, 'post_type' => 'page' ));
if( $subs->have_posts() ) : while( $subs->have_posts() ) : $subs->the_post(); ?>
<?php if (($c % 3) === 1) {
// This creates part of the wrapper .row div
echo "<div class='row'>";
} ?>
<?php
if ($c == 1) {?>
<div class="col_4 card bar">
first card that is manually inserted with different content
</div>
<?php } ?>
<a href="<?php the_permalink(); ?>" class="col_4 card bar no-pad <?php if($c % 3 == 0) { echo 'last'; } ?>">
<?php if ( has_post_thumbnail() ) {?>
<div class="feature-image c-1">
<?php the_post_thumbnail('medium'); ?>
</div>
<?php } ?>
<div class="excerpt-wrap">
This is a post from within Wordpress
</div>
</a>
<?php if ($c == 6) {?>
<div class="col_4 card bar">
Last card that is manually inserted with different content
</div>
<?php } ?>
<?php if (($c % 4) === 3) {
echo "</div>";
}?>
<?php $c++; endwhile; endif; wp_reset_postdata(); ?>
编辑 这是我想要实现的 HTML 结构:
<!-- very first row -->
<div class="row">
<!-- This is a static block followed by the very first two worpdress posts-->
<div class="static-block"></div>
<a href="#" class="wp-post"></a>
<a href="#" class="wp-post"></a>
</div>
<!-- I could have 3 or 30 wordpress posts repeating this format -->
<div class="row">
<a href="#" class="wp-post"></a>
<a href="#" class="wp-post"></a>
<a href="#" class="wp-post"></a>
</div>
<!-- very last row -->
<div class="row">
<!-- These are the very two worpdress posts followed by a static block -->
<a href="#" class="wp-post"></a>
<a href="#" class="wp-post"></a>
<div class="static-block"></div>
</div>
【问题讨论】:
-
不太清楚您要做什么。 stackoverflow.com/help/mcve
-
@treegarden 我添加了一张图片,希望能让事情更清晰
-
你的代码有什么问题?
-
@treegarden 我也将这个添加到问题中,在那里我介绍了我的 PHP。目前正在添加 4 行而不是仅 3 行
-
@treegarden 我还上传了我想要实现的 HTML 结构
标签: php html css wordpress loops