dtq7387 2015-01-27 13:08
浏览 47
已采纳

如何使用自定义标记和层次结构输出类别?

On this SO question I have found the following

$args = array(
 'orderby' => 'name',
 'order' => 'ASC',
 'number' => 20 // how many categories
);
$categories = get_categories($args);
foreach($categories as $category) { 
  echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>&rsaquo;';
} 

That's a step but that it isn't creating a hierarchy. I have read get categories in wp codex but I couldn't work it out.

My structure is:

CAT 1
  cat 1.1
  cat 1.2
CAT 2
  cat 2.1
  cat 2.2

Tried this but the output is simply a list of items with no hierarchy at all:

public function walk_taxonomy( $type = "checkbox", $args = array() ) {
    $args['walker'] = new Search_Filter_Taxonomy_Walker($type, $args['name']);
    $output = $argo = array(
        'hierarchical' => 1
        );
        $categories = get_categories($argo);
        foreach($categories as $category) { 
            echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>&rsaquo;';
        } 
    if ( $output )
        return $output;
}
  • 写回答

1条回答 默认 最新

  • douyong1905 2015-01-27 13:37
    关注

    you have to add this to the args:

    'hierarchical' => 1,
    

    in your foreach you have to check if $category->parent is empty or not.. if not empty indent by adding spaces or applying a class to your li like

    <li class="indent">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答