$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Theme development - Automatically menu creation|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Theme development - Automatically menu creation

matteradmin8PV0评论

I've been working for a couple of days with automatic creation of menu upon theme activation in wordpress. I've achieved the creation of menu and it's working as expected, except i want to add sub items to some of menu items. For that moment i have

  • Home
  • Menu item 1
  • Menu item 2

What I'm trying to achieve is:

  • Home
  • Menu item 1 - Sub item 1, - Sub item 2
  • Menu item 2

I've been searching for the whole day without result. Could anybody help?

Here is my code so far:

add_action('after_switch_theme', 'my_after_switch_theme');
add_action('after_setup_theme', 'my_after_setup_theme');

function my_after_setup_theme()
{
    register_nav_menus(array(
        'primary' => __('Main Menu', ''),
    ));
}

function my_after_switch_theme()
{

    $menu_check = get_option('menu_check');
    if (!$menu_check)
    {
        $primary_menu_items = array(
            'Home' => '/',
            'Menu item 2' => '/',
            'Menu item 3' => '/',
        );
        generate_nav_menu('Primary Menu', $primary_menu_items, 'primary');

    }
}

function generate_nav_menu_item($term_id, $title, $url)
{

    wp_update_nav_menu_item($term_id, 0, array(
        'menu-item-title' => sprintf(__('%s', 'text_domain'), $title),
        'menu-item-url' => home_url('/' . $url),
        'menu-item-status' => 'publish'
        ));
        }

function generate_nav_menu($menu_name, $menu_items_array, $location_target)
{
    $menu_primary = $menu_name;
    wp_create_nav_menu($menu_primary);
    $menu_primary_obj = get_term_by('name', $menu_primary, 'nav_menu');
foreach ($menu_items_array as $page_name => $page_location)
{
    generate_nav_menu_item($menu_primary_obj->term_id, $page_name, $page_location);
}

$locations_primary_arr = get_theme_mod('nav_menu_locations');
$locations_primary_arr[$location_target] = $menu_primary_obj->term_id;
set_theme_mod('nav_menu_locations', $locations_primary_arr);

//update_option('menu_check', true);

}

I've been working for a couple of days with automatic creation of menu upon theme activation in wordpress. I've achieved the creation of menu and it's working as expected, except i want to add sub items to some of menu items. For that moment i have

  • Home
  • Menu item 1
  • Menu item 2

What I'm trying to achieve is:

  • Home
  • Menu item 1 - Sub item 1, - Sub item 2
  • Menu item 2

I've been searching for the whole day without result. Could anybody help?

Here is my code so far:

add_action('after_switch_theme', 'my_after_switch_theme');
add_action('after_setup_theme', 'my_after_setup_theme');

function my_after_setup_theme()
{
    register_nav_menus(array(
        'primary' => __('Main Menu', ''),
    ));
}

function my_after_switch_theme()
{

    $menu_check = get_option('menu_check');
    if (!$menu_check)
    {
        $primary_menu_items = array(
            'Home' => '/',
            'Menu item 2' => '/',
            'Menu item 3' => '/',
        );
        generate_nav_menu('Primary Menu', $primary_menu_items, 'primary');

    }
}

function generate_nav_menu_item($term_id, $title, $url)
{

    wp_update_nav_menu_item($term_id, 0, array(
        'menu-item-title' => sprintf(__('%s', 'text_domain'), $title),
        'menu-item-url' => home_url('/' . $url),
        'menu-item-status' => 'publish'
        ));
        }

function generate_nav_menu($menu_name, $menu_items_array, $location_target)
{
    $menu_primary = $menu_name;
    wp_create_nav_menu($menu_primary);
    $menu_primary_obj = get_term_by('name', $menu_primary, 'nav_menu');
foreach ($menu_items_array as $page_name => $page_location)
{
    generate_nav_menu_item($menu_primary_obj->term_id, $page_name, $page_location);
}

$locations_primary_arr = get_theme_mod('nav_menu_locations');
$locations_primary_arr[$location_target] = $menu_primary_obj->term_id;
set_theme_mod('nav_menu_locations', $locations_primary_arr);

//update_option('menu_check', true);

}
Share Improve this question asked Mar 14, 2019 at 15:38 AnTrakSAnTrakS 1033 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Looking at the code for wp_update_nav_menu_item() the third parameter is an array of data about the menu item you are trying to update/create. One of the accepted items in that array is menu-item-parent-id. If you pass that key with the value of the ID of the parent menu item as part of the menu item data array then your problem should be solved.

EDIT: I haven't tested this, but in theory, and assuming everything you had before was correct, this will work for you

add_action('after_switch_theme', 'my_after_switch_theme');
add_action('after_setup_theme', 'my_after_setup_theme');

function my_after_setup_theme()
{
    register_nav_menus(array(
                           'primary' => __('Main Menu', ''),
                       ));
}

function my_after_switch_theme()
{

    $menu_check = get_option('menu_check');
    if (!$menu_check) {
        $primary_menu_items = array(
            'Home'        => array('url' => '/'),
            'Menu item 2' => array(
                'url' => '/',
                'sub-items' => array(
                    'Sub item 1' => array('url' => '/'),
                    'Sub item 2' => array('url' => '/')
                )),
            'Menu item 3' => array('url' => '/'),
        );
        generate_nav_menu('Primary Menu', $primary_menu_items, 'primary');

    }
}

function generate_nav_menu_item($term_id, $title, $data, $parent = false)
{
    if (!$data['url']) {
        return;
    }

    $args = array(
        'menu-item-title'  => sprintf(__('%s', 'text_domain'), $title),
        'menu-item-url'    => home_url('/' . $data['url']),
        'menu-item-status' => 'publish'
    );
    if ($parent) {
        $args['menu-item-parent-id'] = $parent;
    }

    $item_id = wp_update_nav_menu_item($term_id, 0, $args );
    if (!empty($data['sub-items']) && !is_wp_error($item_id)) {
        foreach ($data['sub-items'] as $sub_item_title => $item) {
            generate_nav_menu_item($term_id, $sub_item_title, $item, $item_id);
        }
    }
}

function generate_nav_menu($menu_name, $menu_items_array, $location_target)
{
    $menu_primary = $menu_name;
    wp_create_nav_menu($menu_primary);
    $menu_primary_obj = get_term_by('name', $menu_primary, 'nav_menu');
    foreach ($menu_items_array as $page_name => $page_data) {
        generate_nav_menu_item($menu_primary_obj->term_id, $page_name, $page_data);
    }

    $locations_primary_arr = get_theme_mod('nav_menu_locations');
    $locations_primary_arr[$location_target] = $menu_primary_obj->term_id;
    set_theme_mod('nav_menu_locations', $locations_primary_arr);

//update_option('menu_check', true);

}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far