首頁>Web>source

我正在尝試按字母順序在各列中的字母首字母下print"品牌"名稱的列表.問题是我要print三个帶有三个品牌的列,如所附圖像所示。

<?php
$list = '';
$groups = array();
$tags = get_terms('brand',$args);
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
  $first_letter = strtoupper( $tag->name[0] );
  $groups[ $first_letter ][] = $tag;
}
if( !empty( $groups ) ) {
  foreach( $groups as $letter => $tags ) {
    $list .= '<div><h3>' . $letter . '</h3>';
    $counterCustom = 0; //
    foreach( $tags as $tag ) {
    if ($counterCustom == 0 ) { //
        $list .= '<div class="container"><div class="row"><div class="col-lg-4"><ul><li><a href="/brand/'.$tag->slug.'">'.$tag->name.'</a></li>';
    }
    if($counterCustom == 1) { //
        $list .= '<li><a href="/brand/'.$tag->slug.'">'.$tag->name.'</a></li>';
    } //
    elseif($counterCustom == 2 ) { //
        $list .= '<li><a href="/brand/'.$tag->slug.'">'.$tag->name.'</a></li>';
    } //
    elseif($counterCustom == 3 ) { //
        $list .= '</ul></div><div class="col-lg-4"><ul><li><a href="/brand/'.$tag->slug.'">'.$tag->name.'</a></li>';
    } //
    elseif($counterCustom == 4) { //
        $list .= '<li><a href="/brand/'.$tag->slug.'">'.$tag->name.'</a></li>';
    } //
    elseif($counterCustom == 5) { //
        $list .= '<li><a href="/brand/'.$tag->slug.'">'.$tag->name.'</a></li></ul></div><div class="col-lg-4"><ul>';
    } //
    elseif($counterCustom == 6) { //
        $list .= '<li><a href="/brand/'.$tag->slug.'">'.$tag->name.'</a></li>';
    } //
    elseif($counterCustom == 7) { //
        $list .= '<li><a href="/brand/'.$tag->slug.'">'.$tag->name.'</a></li>';
    } //
    elseif($counterCustom == 8) { //
        $list .= '<li><a href="/brand/'.$tag->slug.'">'.$tag->name.'</a></li></ul></div></div></div>';
    } //
    $counterCustom++;
      }
    $list .='';
  }
}
echo $list;
?>

這就是我想要做的:

最新回復
  • 8月前
    1 #

    让CSS處理網格佈局.

    例如具有這樣的標記:

    <ul class="term-list">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
    </ul>
    

    您可以仅使用以下CSS建立该佈局:

    .term-list {
      display: grid;
      grid-template-columns: repeat(3, minmax(0,1fr));
      column-gap: 1rem;
      grid-gap: 1rem;
    }
    

    因此,只需放棄自定義計數器,每个迴圈仅附加 <li>

    foreach( $groups as $letter => $tags ) {
        $list .= '<div><h3>' . $letter . '</h3> <ul>';
        foreach( $tags as $tag ) {
            $list .= '<li><a href="/brand/'.$tag->slug.'">'.$tag->name.'</a></li>';
        }
        
        $list .= '</ul></div>';
    }
    

  • drupal:获取当前节點的父選單項
  • drupal:將使用者物件傳遞给Token :: replace