首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PHP循环中插入静态块

在PHP循环中插入静态块
EN

Stack Overflow用户
提问于 2015-06-12 09:22:41
回答 2查看 1.5K关注 0票数 4

我试图在一个循环中插入2个静态div,特别是一个在循环的最开始,另一个在最后。

这两个DIV必须出现在他们相应的.row父级中,目前它每3个DIV包一次。我怎么做呢?

编辑这里有一个图像来描述我所需要的,粉红色块是手动插入的div,将有不同的内容与蓝色的div。那些蓝色的div只是WP的帖子:

这里是我的PHP,目前它在第一行和最后一行中创建了4列,其中应该只有3列:

代码语言:javascript
复制
<?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(); ?>

编辑这是我想要实现的结构:

代码语言:javascript
复制
<!-- 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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-12 11:21:57

代码语言:javascript
复制
<?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(); 
?>

如果有超过7页的页面,而您想要静态块,最后添加,只需更改7到post计数值( $c == $sub->post_ count )

票数 3
EN

Stack Overflow用户

发布于 2015-06-12 09:43:25

编辑:对不起,在你上传线框图片之前,我看过你的帖子。

我不太清楚你代码的目标。但是,我所理解的是,你需要生成这样的结构:

代码语言:javascript
复制
<div class='row'>
  <div class='static'>
  </div>
  <div class='static'>
  </div>

  #here the loop will create
  <div></div>
  <div></div>
  <div></div>

  <div class='static'>
  </div>
  <div class='static'>
  </div>
</div>

在你这段时间里,这会被复制到很多我身上。

如果这是您所需要的,那么我认为您需要使用$c变量来计算1,2,3。每次您在$c =1中打印前2个静态div,当您在$c =3时打印最后2个静态div。当您到达$c =3时,将$c重置为1,并包含一个条件询问,询问它是否是最后一个项和它的$c != 3,因此您打印出最后2个静态div。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30799598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档