如何在两列中显示wordpress类别?

时间:2015-09-27 06:31:23

标签: php mysql wordpress categories

我想在当时显示我的类别2 - 每行2个,并根据需要显示多行。

到目前为止,我已经使用此代码(显示所有类别):

<?php
$i = 1;
$args = array(
  'orderby' => 'name',
  'parent' => 0,
  'hide_empty' => 0
  );
$categories = get_categories( $args );

echo count( $categories );

echo "<div class='a'>\n";
foreach ( $categories as $category ) {
    echo "<div class='b'>\n";
    echo "<div class='c'><a href='" . get_category_link( $category->term_id ) . "'>" . $category->name . "</a></div>\n";
    echo "</div>\n";
}
echo "</div>\n";
?>

我知道可以使用list完成,但这是我想要避免的。

1 个答案:

答案 0 :(得分:0)

所以这就是它(需要一段时间),它运作得很好; - )

$args = array(
  'orderby' => 'name',
  'parent' => 0,
  'hide_empty' => 1
  );
$categories = get_categories( $args );
$list_cats = array();
foreach ( $categories as $category ) {
    array_push($list_cats, $category->name);
}
$total_cats=count($categories);

echo "<div class='a'>";
for ($a=0; $a<$total_cats;) { 
   for ($j=0; $j<2; $j++) { 
            echo " <div class='b'><div class='c'>" . $list_cats[$a] . "</div></div>"; 
       $a++;
   } 
   echo "</div>"; 
   if($a<$total_cats){
            echo "<div class='a'>"; 
   }

}