子类别的下拉菜单

时间:2018-02-22 作者:Martin

此代码显示所有可用类别,以及在其相应父类别的正下方列出的子类别。是否可以制作一个嵌套菜单,以便当我单击父类别时,子类别在父类别的正下方向下滚动/下拉?

$args = array(
    \'child_of\'            => 0,
    \'current_category\'    => 0,
    \'depth\'               => 0,
    \'echo\'                => 1,
    \'exclude\'             => \'\',
    \'exclude_tree\'        => \'\',
    \'feed\'                => \'\',
    \'feed_image\'          => \'\',
    \'feed_type\'           => \'\',
    \'hide_empty\'          => 0,
    \'hide_title_if_empty\' => false,
    \'hierarchical\'        => true,
    \'order\'               => \'ASC\',
    \'orderby\'             => \'name\',
    \'separator\'           => \'<br />\',
    \'show_count\'          => 0,
    \'show_option_all\'     => \'\',
    \'show_option_none\'    => __( \'No categories\' ),
    \'style\'               => \'list\',
    \'taxonomy\'            => \'category\',
    \'title_li\'            => __( \'Categories\' ),
    \'use_desc_for_title\'  => 1,
);

$list_categories = get_categories( $args );
echo "<div class=\'container\'>";
echo "<div class=\'productsubmenu\'>";
echo "<ul>";

foreach( $list_categories as $cat ) {

    $cat_id = $cat->term_id;
    $args2  = array(
        \'child_of\'            => $cat_id,
        \'current_category\'    => 0,
        \'depth\'               => 0,
        \'echo\'                => 1,
        \'exclude\'             => \'\',
        \'exclude_tree\'        => \'\',
        \'feed\'                => \'\',
        \'feed_image\'          => \'\',
        \'feed_type\'           => \'\',
        \'hide_empty\'          => 0,
        \'hide_title_if_empty\' => false,
        \'hierarchical\'        => true,
        \'order\'               => \'ASC\',
        \'orderby\'             => \'name\',
        \'separator\'           => \'<br />\',
        \'show_count\'          => 0,
        \'show_option_all\'     => \'\',
        \'show_option_none\'    => __( \'No categories\' ),
        \'style\'               => \'list\',
        \'taxonomy\'            => \'category\',
        \'title_li\'            => __( \'Categories\' ),
        \'use_desc_for_title\'  => 1,
    );


    if( $cat->category_parent == 0 ) {

        $category_id = $cat->term_id;

        echo \'<li class="parentss"><a href="\'.get_term_link( $cat ) . \'">\' . $cat->name . \'</a>\';


        $subcats = get_categories( $args2 );
        echo \'<ul class="wooc_sclist">\';

        foreach( $subcats as $sc ) {

            $link = get_term_link( $sc->slug, $sc->taxonomy );
            echo \'<li class="" ><a href="\'. $link .\'">\'.$sc->name.\'</a></li>\';
            var_dump( cat_is_ancestor_of($cat_id,32));
            var_dump( $cat_id);

        }

    }

    echo \'</ul>\';
    echo "</li>";

}

1 个回复
SO网友:obiPlabon

您不需要创建类别列表功能,WordPress已经创建了一个帮助器函数来轻松处理这个问题。看看wp_list_categories(). 如果需要的话,可以通过一些CSS和一点JS来实现您所需要的。你会发现很多jQuery菜单插件,你可以使用其中的任何一个,或者如果你需要一个树状结构,那么检查一下https://www.jstree.com/

结束

相关推荐

按帖子类型筛选wp_Dropdown_Categories

我正在使用wp\\u dropdown\\u categories向媒体库添加类别过滤器。它工作正常,除了显示分配给帖子的所有类别,但我希望它只显示分配给附件的类别。如果只为附件更新计数也很好。下面是将类别添加到附件并允许您对其进行筛选的代码。/* Add categories to attachments/media library */ function wptp_add_categories_to_attachments() { register_taxonomy_for_objec