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; ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化