8

我目前正在使用它来列出所有类别:

<?php 
    $args = array (
    'menu' => 'secondary-navigation',
    'sort_column' => 'menu_order',
    'container' => 'false', 
    'title_li' => 0,
    'hide_empty' => 0
    );
    wp_list_categories($args);
?>

这将简单地列出层次结构中的所有类别并锚定每个项目。

我的类别实际上是这样设置的:

Parent
    -> Child
        -> Grandchild
            -> Great Grandchild

我的问题是我只希望曾孙有主播。我不希望父母、孩子或孙子有锚。

任何建议,将不胜感激。

4

3 回答 3

1

正如 Lleo Holmes 在评论中提到的,最好的方法是创建一个自定义的Walker 类来实现这个功能。我试了一下,得出以下结论:

class Depth_Category_Walker extends Walker_Category {
    private $allowed_depths;

    function __construct( $depths_to_link = array() ) {
        $this->allowed_depths = !is_array($depths_to_link) ? array($depths_to_link) : $depths_to_link;
    }

    function start_el( &$output, $category, $current_depth = 0, $args = array(), $id = 0 ) {
        extract($args);
        if( in_array( $current_depth, $this->allowed_depths ) ) {
            parent::start_el( $output, $category, $current_depth, $args, $id );
        } else {
            $cat_name = esc_attr( $category->name );
            $cat_name = apply_filters( 'list_cats', $cat_name, $category );
            if ( !empty($show_count) )
                $cat_name .= ' (' . intval($category->count) . ')';

            if ( 'list' == $args['style'] ) {
                $output .= "\t<li";
                $class = 'cat-item cat-item-' . $category->term_id;
                if ( !empty($current_category) ) {
                    $_current_category = get_term( $current_category, $category->taxonomy );
                    if ( $category->term_id == $current_category )
                        $class .=  ' current-cat';
                    elseif ( $category->term_id == $_current_category->parent )
                        $class .=  ' current-cat-parent';
                }
                $output .=  ' class="' . $class . '"';
                $output .= ">$cat_name\n";
            } else {
                $output .= "\t$cat_name<br />\n";
            }
        }
    }
}

这扩展了Walker_Category类,以便我们可以调用parent::start_el()以在适当的深度生成链接。构造函数接受包含您希望显示链接的级别的深度数组。超出该数组的任何深度都将呈现为纯文本。请注意,else代码取自Walker_Category::start_el,因此如果基类被修改过,这可能会在未来的版本中中断。

上面的类可以通过调用来使用wp_list_categories

<?php
   $args = array(
       'orderby' => 'menu_order',
       'title_li' => 0,
       'hide_empty' => 0,
       'walker' => new Depth_Category_Walker(2)
   );
?>
<ul>
   <?php wp_list_categories( $args ); ?>
</ul>
于 2013-03-29T14:19:45.197 回答
1

您可以简单地使用纯 CSS 禁用链接,而无需更改任何 php 代码。检查以下代码,它会改变鼠标光标,禁用链接功能,隐藏下划线样式:

.cat-item a, .cat-item .cat-item .cat-item .cat-item a {
      cursor: default;
      pointer-events: none;
      text-decoration: none;
}
.cat-item .cat-item .cat-item a {
      cursor: pointer;
      pointer-events: auto;
      text-decoration: underline;
      /* also add here any special style for grandchildren categories */
}

结果将完全符合您的要求,只有孙类别似乎被锚定。

希望这能回答你的问题

于 2013-02-27T00:16:18.087 回答
0

请使用此代码

$categories = get_categories(array(
        'child_of' => 9,
        'hide_empty' => false
    ));
foreach($categories as $cat){
    echo $cat->name;
    echo '<br />';
}

此代码仅针对二级类别进行测试。

将 9 替换为您的类别 ID,例如“孙子”类别 ID。此代码未经测试,但这是工作

希望这对你有帮助

于 2013-01-02T05:55:09.973 回答