如何在WordPress导航菜单中自动添加类别和子类别

6
我有一个很长的类别列表,包括子类别和子类别的子类别。在WordPress的菜单部分逐一选择并重新排列类别及其子类别的层次结构需要很长时间。
我搜索了相关信息,但没有找到任何有用的内容。所以我想知道是否有任何函数或插件可以自动添加这些类别到菜单中。
谢谢。

制作自己的菜单遍历器(https://codex.wordpress.org/Class_Reference/Walker)。 - Aibrean
我真的很想要这个。 - Jovylle
1
2020年了,这个问题仍然很重要!WordPress核心团队什么时候才能解决这个基本功能呢 :) - Manitra Andriamitondra
4个回答

4

1
仍然非常好用,适用于WordPress 5.5.1。网站上似乎不再提供文档。但是如果您打开WP菜单设置,菜单项本身就有一个JC子菜单选项,您可以从文章类型、类别等中填充。 - RemBem

4

我使用了 JC Submenu 插件,并通过在主题 functions.php 中添加过滤器来使其正常工作。非常完美。我使用了 Astra 主题。

//Enable compatibility with theme custom menu walkers

add_filter('jcs/enable_public_walker', 'jc_disable_public_walker');

function jc_disable_public_walker($default){
    return false;
}

谢谢,节省了我两个小时。与REHub很好地配合使用! - Stz
非常感谢!!!你救了我!!!它真的有效!我还添加了来自wordpress.org/support/topic/woocommerce-product-categories-menu-2的代码,感谢那个人! - Hottabov
这个插件已经一年没有更新了。而且它与最新版本的WordPress不兼容。 - Yisal Khan

0

WordPress.org上的菜单插件,可以帮助您在WordPress导航菜单中使用短代码自动添加分层分类。


1
请提供一个示例,而不仅仅是从某个来源发布链接。 - dbf

0
如果有人仍在寻找无需插件的解决方案,我使用 wp_nav_menu walker 实现了这个可行版本。
基本上,我只在我的外观 > 菜单中放置一级分类,并在我的 start_el 函数中循环遍历这些分类的文章。
以下是我的 walker 函数:
class AWP_Menu_Walker extends Walker_Nav_Menu {

    function start_el(&$output, $item, $depth=0, $args=[], $id=0) {
        $open = '';
        $current = '';
        $sub_menu = '';
        $menuItemFont = 'font-sans text-sm font-light ';
        $subMenuItemFont = 'text-sm font-sans font-light ';
        $current_element_array = array( 'current-menu-item' );
        $current_element_markers = array( 'current-menu-item', 'current-menu-parent', 'current-menu-ancestor' );
        $current_class = array_intersect( $current_element_markers, $item->classes );
        $current_element = array_intersect( $current_element_array, $item->classes );

        if ($item->object == 'category') {
            $category_slug = get_category($item->object_id);
            $args = array(
                'tag' => get_queried_object()->slug,
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'orderby' => 'menu_order',
                'order' => 'ASC',
                'tax_query' => array(
                    array(
                            'taxonomy' => 'category',
                            'field'    => 'slug',
                            'terms'    => $category_slug->slug, // here I get the slug of indexed category 
                    ),
                )
            );
            $posts = get_posts( $args );
            $current_url = get_permalink();
            foreach ($posts as $post) :
                $post_url = get_permalink($post->ID);
                if ($post_url == $current_url) {
                    $current = 'text-red-500 ';
                }
                $sub_menu .= '<li class="select-none my-2 truncate- leading-tight ' . $current . $subMenuItemFont . ' px-4 pl-6"><a title="' . esc_html($post->post_title) . '" href="' . $post_url . '">' . esc_html($post->post_title) . '</a></li>';
            endforeach;
        }

        // conditional styles for menu items
        if (!empty($current_element)) {
            $menuItemFont .= 'text-red-500 ';
            $subMenuItemFont .= 'text-red-500 ';
        } else {
            $menuItemFont .= 'text-zinc-800 ';
            $subMenuItemFont .= 'text-zinc-600 ';
        }

        // This set the <details> open if current item is whitin
        if (!empty($current_class)) {
            $open = 'open';
        }

        if (!$args->walker->has_children) {
            if ($item->menu_item_parent == 0){
                if ($item->object == 'category') {
                    $output .= '<details ' . $open . ' class="' . $menuItemFont . ' pl-4">';
                    $output .= '<summary class="select-none cursor-pointer truncate- ' . $menuItemFont . $color . $current . '"><span class="pl-4">' . esc_html($item->title) . '</span></summary>';
                    $output .= $sub_menu;
                } else {
                    $output .= '<li class="select-none truncate- ' . $current . $menuItemFont . $color . ' pl-8 pr-2"><a title="' . esc_html($item->title) . '" href="' . esc_url($item->url) . '">' . esc_html($item->title) . '</a></li>';
                }
            }
        } else {
            $output .= '<details ' . $open . ' class="' . $menuItemFont . ' pl-4">';
            $output .= '<summary class="select-none cursor-pointer truncate- ' . $menuItemFont . $color . $current . '"><span class="pl-4">' . esc_html($item->title) . '</span></summary>';
        }
    }

    function end_el(&$output, $item, $depth = 0, $args = Array()){ // closing li a span
        if ($item->menu_item_parent == 0){
            $output .= '</details>';
        }
        if ($args->walker->has_children) {
            $output .= '</details>';
        }
    }
}
/* Walker nav menu */

然后我在我的模板中调用它:
    wp_nav_menu(
    array(
        'theme_location' => 'menu-1',
        'menu_id'        => 'primary-menu',
        'items_wrap'     => '<ul id="%1$s" class="%2$s font-display flex flex-col gap-1" aria-label="submenu">%3$s</ul>',
        'walker'         => new AWP_Menu_Walker(),
    )
);

我在菜单中设置了我的分类。

I set my categories in the menu

这是结果:

This is the result


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接