在PHP循环中插入静态块

时间:2015-06-12 09:22:41

标签: php html css wordpress loops

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

这两个div必须出现在相应的.row父级中,这个父级目前包含每3个DIV。我怎么能这样做?

修改 这是描述我需要的图像,粉色块是手动插入的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>

2 个答案:

答案 0 :(得分:2)

<?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更改为发布计数值($ c == $ sub-&gt; post_count)

enter image description here

答案 1 :(得分:0)

编辑:抱歉,在您上传线框图片之前,我似乎看到了您的帖子。

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

<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>

并且中的 i 将会重复。

如果这就是你需要的,那么我认为你需要做的是用你的$ c变量计算1,2,3。每次你在$ c = 1时打印前2个静态div,当你在$ c = 3时打印最后2个静态div。当你达到$ c = 3时,将$ c重置为1并包含一个条件询问它是否是最后一项及其$ c!= 3,以便打印出最后2个静态div。