2016-08-24 5 views
0

私はwordpressでPHPのカラムにいくつかのデータを表示しようとしています。私はそれを行うには、次のスニペットを持っている:PHPでカラムにデータを表示

$args = array(
    'taxonomy'  => $taxonomy, 
    'orderby'  => $orderby, 
    'show_count' => $show_count, 
    'pad_counts' => $pad_counts, 
    'hierarchical' => $hierarchical, 
    'title_li'  => $title, 
    'parent'  =>19, 
    'hide_empty' => $empty 
); 
$columnas=0; 
$all_categories = get_categories($args); 
echo '<table><tr>'; 
foreach ($all_categories as $cat) { 
    $category_id = $cat->term_id;  
    if((($columnas%4)!==0)and($columnas!==0)){ 
    echo '<td><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></td>'; 
    $columnas=$columnas+1;  
    echo 'Las columnas son: '.$columnas.'<br/>'; 
    }else 
    { 
    echo 'Columnas es: '.$columnas; 
    echo '<td><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></td></tr><tr>'; 
    } 


} 
echo '</tr></table>'; 

だから私が間違ってやっている?私は4つの列と多くの行を持つテーブルである私が欲しいものを得ることはありません。確かに、それはばかげたことですが、私はそれを見ません。

+0

私はあなただけのカラムカラム== 0 – Fels

+1

'$カラムカラム= $カラムカラム+ 1そうでない場合、あなたが列を閉じ...((($カラムカラム%4)場合は、他に追加する必要があると思います;' '$ columnas ++;'にしてif文の外側に置いてください。さもなければそれは決して増加しません –

答えて

0

カテゴリを4列に表示する必要があると仮定します。それでは、このコードは4列目のテーブルに表示するのに役立ちます。

$args = array(
'taxonomy'  => $taxonomy, 
'orderby'  => $orderby, 
'show_count' => $show_count, 
'pad_counts' => $pad_counts, 
'hierarchical' => $hierarchical, 
'title_li'  => $title, 
'parent'  =>19, 
'hide_empty' => $empty 
); 
$columnas=0; 
$all_categories = get_categories($args); 

echo '<table><tr>'; 
foreach($all_categories as $cat) { 
    if(($columnas%4) == 0 && $columnas > 0) 
     echo '</tr><tr>'; 
    else 

    $category_id = $cat->term_id; 
    echo '<td><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></td>'; 
    $columnas++; 
} 
$cnt = (($columnas + 1)%4); 
if($cnt > 0) { 
    echo '<td colspan="' . (($columnas-1) - $cnt) . '"></td>'; 
} 
echo '</tr></table>'; 

しかし、私のsuggetionあなたは、UL李のための4XリストGORに表示しようとしています。簡単にループを行う。

おかげ

0

ここでは、あなたのコードを入力する必要があります、私はそれがあなたを助けてくれることを願っています。

$all_categories = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; 
    $html = '<table><tr>'; 
    $i = 0; 

    foreach ($all_categories as $cat) { 
     if (($i % 4) == 0 && ($i != 0)) { 
      $html .="</tr><tr>"; 
     } 
     $html .="<td>$cat</td>"; 
     $i++; 
    } 
    $html .= '</tr></table>'; 
    echo $html; 
関連する問題