gpt4 book ai didi

php - 在 PHP 循环中插入静态 block

转载 作者:可可西里 更新时间:2023-10-31 23:03:58 30 4
gpt4 key购买 nike

我试图在 PHP 循环中插入 2 个静态 div,具体来说,一个在循环的开头,一个在循环的结尾。

这 2 个 div 必须出现在它们相应的 .row 父级中,该父级当前环绕每 3 个 DIV。我该怎么做?

编辑这是描述我需要的图像,粉红色 block 是手动插入的 div,其内容与蓝色 div 不同。那些蓝色的 div 只是 WP 帖子:

enter image description here

这是我的 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>

最佳答案

<?php
$c = 1;
$subs = new WP_Query(array('post_parent' => 14, 'post_type' => 'page'));
if ($subs->have_posts()) :
?>
<div class='row'>
<?php
while ($subs->have_posts()) : $subs->the_post();
if (($c % 3) == 0 || $c == 3):
?>
</div><div class='row'>
<?php
endif;
?>
<?php
if ($c == 1):
?>
<div class="col_4 card bar">
first card that is manually inserted with different content
</div>
<?php endif; ?>

<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 == 7) { ?>
<div class="col_4 card bar">
Last card that is manually inserted with different content
</div>
<?php } ?>


<?php
$c++;
endwhile;
?>
</div>
<?php
endif;
wp_reset_postdata();
?>

if have more then 7 page and u want to static block add on last just change 7 to post count value ( $c == $sub->post_count)

enter image description here

关于php - 在 PHP 循环中插入静态 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30799598/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com