【问题标题】:Insert static blocks within a PHP loop在 PHP 循环中插入静态块
【发布时间】: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


【解决方案1】:
<?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->post_count)

【讨论】:

  • 这非常有效。正是我想要的!谢谢拉维!
  • 快速提问:使用您的代码,如何为所有块设置全局计数器?
【解决方案2】:

编辑:抱歉,我好像在你上传线框图片之前看到了你的帖子。

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

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

这将在您的 while 中复制尽可能多的 i

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

【讨论】:

  • 在看到你刚刚上传的图片之后,我给出的解决方案保持不变,只是数 4 而不是 3,并在开头和结尾添加 1 个 div。希望这是您所需要的。
  • 感谢您的回答。用理想的 HTML 结构查看我更新的问题
  • 在这种情况下,您需要获取数组的长度.. 剩余 2(第一行)并将剩余的除以 3。结果将是您将在中间(没有打印出静态 div).. 该分区的其余部分将是剩余的 1 或 2 个 div(您将使用 ceil(i*3) 将其四舍五入),最后一行您将拥有你需要计算它,当你处于那个位置时,你会知道你必须打印出最后一个静态 div。如果项目数为 32,您必须考虑会发生什么。最后一个静态项目会发生什么?
  • 能否提供一些示例代码?我不完全确定我明白你的意思。
猜你喜欢
  • 2014-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-28
  • 2011-11-13
  • 2012-12-20
相关资源
最近更新 更多