在php列表中动态添加div

时间:2017-10-27 12:05:27

标签: php html wordpress

我有一个由以下代码创建的列表:

$facilities_bid = get_the_term_list( 
                  $post->ID, 
                  'room_facilities', 
                  '<ul class="fac-styles">
                    <li>
                    <i class="fa fa-circle-o" aria-hidden="true">', '</i>
                    </li>
                    <li>
                    <i class="fa fa-circle-o" aria-hidden="true">', '</i>
                    </li>
                   </ul>' );
echo strip_tags($facilities_bid, '<ul><li><i>');

HTML输出是:

 <ul class="fac-styles">
  <li><i class="fa fa-circle-o" aria-hidden="true">Air Conditioning</i></li>
  <li><i class="fa fa-circle-o" aria-hidden="true">Coffee machine</i></li>
  <li><i class="fa fa-circle-o" aria-hidden="true">Flat-screen TV</i></li>
  <li><i class="fa fa-circle-o" aria-hidden="true">Free WiFi</i></li>
  <li><i class="fa fa-circle-o" aria-hidden="true">Hairdryer</i></li
  <li><i class="fa fa-circle-o" aria-hidden="true">Telephone</i></li>
  </ul>

我希望列表是这样的,php会在每三个项后自动创建嵌套的div:

  <div class="one">
  <ul class="fac-styles">
  <li><i class="fa fa-circle-o" aria-hidden="true">Air Conditioning</i></li>
  <li><i class="fa fa-circle-o" aria-hidden="true">Coffee machine</i></li>
  <li><i class="fa fa-circle-o" aria-hidden="true">Flat-screen TV</i></li>
  </ul>
  </div>
  <div class="one">
  <ul class="fac-styles">
  <li><i class="fa fa-circle-o" aria-hidden="true">Free WiFi</i></li>
  <li><i class="fa fa-circle-o" aria-hidden="true">Hairdryer</i></li
  <li><i class="fa fa-circle-o" aria-hidden="true">Telephone</i></li>
  </ul>
  </div>

2 个答案:

答案 0 :(得分:1)

我没有测试过这个并且我从未玩过“get_the_term_list”,但是这会有用吗?

$list = get_the_term_list( $post->ID, 'room_facilities', null, '|', null );
foreach( array_chunk( explode( '|', $list ), 3 ) as $outer )
{
  echo '<ul>';
  foreach( $outer as $inner )
  {
    echo '<li>' . $inner . '</li>';
  }
  echo '</ul>';
}

答案 1 :(得分:0)

尝试在代码中使用小计数器,该计数器考虑嵌套div的位置。 f.e。

$gtnum=0;
    foreach() {    
    $gtnum++;
    if ($gtnum==0) $divstart='<div>'; else $divstart='';
    if ($gtnum==3) {$gtnum=0; $divend='</div>';} else $divend='';
    $facilities_bid = get_the_term_list( 
                          $post->ID, 
                          'room_facilities', 
                          $divstart.'<ul class="fac-styles">
                            <li>
                            <i class="fa fa-circle-o" aria-hidden="true">', '</i>
                            </li>
                            <li>
                            <i class="fa fa-circle-o" aria-hidden="true">', '</i>
                            </li>
                           </ul>'.$divend );
        echo strip_tags($facilities_bid, '<ul><li><i><div>');
    }