dou12754 2015-06-17 12:30
浏览 7
已采纳

带有引导列的PHP计数逻辑

I want to count the custom fields and give the columns the right width to work with Bootstrap 3.

If there are 2 entries I want it to be:

<div class="col-md-6">

If there are 3 entries I want it to be:

<div class="col-md-4">

But it just puts the count number in each div.

EDIT: The solution of "num8er" helped me a lot, THX again! If someone finds this - this is how it works with a normal wordpress loop:

<?php 
if ( have_posts() ): ?> 

<?php $elements = []; 
while ( have_posts() ): the_post(); 
$element = get_the_title(); 
$elements[] = $element;
endwhile; 

if(sizeof($elements)>0) {
    $size = ceil(12 / sizeof($elements));
    if($columns<2) $columns = 2;
        foreach($elements AS $element) {
              echo '<div class="col-md-'.$size.'">'.$element.'</div>';
        }
    }

endif; 
?>

E. g. this is the DOM if i have 6 articles:

<div class="col-md-2"><h2>Homer Simpson’s  Guide  to  Tom Cruise</h2></div>
<div class="col-md-2"><h2>Homer Simpson’s  Guide  to  Tom Cruise</h2></div> 
...
  • 写回答

1条回答 默认 最新

  • dongwei5794 2015-06-17 13:31
    关注

    let's collect the output to array $elements and get the size of column and wrap with it:

    <?php if( have_rows('buehne_inhalt', 'option') ): ?>
    
      <h2><?=the_sub_field('subline')?></h2>
      <div class="row">
      <$php 
      $elements = []; 
      while( have_rows('buehne_inhalt', 'option') ): the_row();
        $image = get_sub_field('buehne_bild');
        $size = 'img-s';
        if( $image ) {
            $element = wp_get_attachment_image( $image, $size );
        }
    
        $elements[] = $element;
      endwhile; 
    
      if(sizeof($elements)>0) {
        $size = ceil(12 / sizeof($elements));
        if($columns<2) $columns = 2;
    
        foreach($elements AS $element) {
          echo '<div class="col-md-'.$size.'">'.$element.'</div>';
        }
      }
      ?>
    </div>
    <?php endif; ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)