月薪1800块的站长
不打算了解一下吗

WordPress菜单导航分类自动添加子分类菜单

在fuction.php中添加

/* 自动给导航菜单中的分类添加其下的子分类 */
add_filter("wp_get_nav_menu_items", function($items, $menu, $args) {
    if (is_admin()) {
        return $items;
    }

    foreach ($items as $index => $i) {
        if ("category" !== $i->object) {
            continue;
        }
        $term_children = get_term_children($i->object_id, "category");
        foreach ($term_children as $index2 => $child_id) {
            $child               = get_term($child_id);
            $url                 = get_term_link($child);
            $e                   = new stdClass();
            $e->title            = $child->name;
            $e->url              = $url;
            $e->menu_order       = 500 * ($index + 1) + $index2;
            $e->post_type        = "nav_menu_item";
            $e->post_status      = "published";
            $e->post_parent      = $i->ID;
            $e->menu_item_parent = $i->ID;
            $e->type             = "custom";
            $e->object           = "custom";
            $e->ID               = 0;
            $e->db_id            = 0;
            $e->object_id        = 0;
            $e->classes          = array();
            $e->description      = "";
            $items[]             = $e;
        }
    }
    return $items;
}, 10, 3);

 

赞(0)
分享到: 更多 (0)