admin管理员组

文章数量:1122846

I try to create a menu item for every new product category created, but it just creates an empty custom menu item (in the right place). If I however don't assign a menu-item-parent-id it works fine (but not in the right place obviously).

function add_product_category_to_menu($term_id, $tt_id, $taxonomy) {
    
    if ($taxonomy === 'product_cat') {
        
        $term = get_term($term_id, $taxonomy);
        $menu_name = 'main-menu';
        $menu = wp_get_nav_menu_object($menu_name);

        if ($menu) {
            $menu_id = $menu->term_id;
            $term_name = $term->name;

            function get_parent_menu_item_id_by_name($parent_id, $menu_id) {
                $parent_term = get_term($parent_id, 'product_cat');
                $menu_items = wp_get_nav_menu_items($menu_id);

                foreach ($menu_items as $menu_item) {
                    if (trim($menu_item->object_id) === trim($parent_term->term_id)) {
                        return $menu_item->ID;
                    }
                }
            }
            
            $menu_item_id = wp_update_nav_menu_item($menu_id, 0, array(
                'menu-item-title' => $term_name,
                'menu-item-object-id' => intval($term_id),
                'menu-item-object' => 'product_cat',
                'menu-item-type' => 'taxonomy',
                'url' => get_term_link($term),
                'menu-item-status' => 'publish'
            ));

            // If the category has a parent, update its parent ID
            if (!empty($term->parent) && $term->parent != 0) {
                $parent_term = get_term($term->parent, 'product_cat');
                
                if (!is_wp_error($parent_term)) {
                    $parent_menu_item_id = get_parent_menu_item_id_by_name($parent_term, $menu_id);
                    error_log('Parent Menu ID: ' . $parent_menu_item_id);
                    wp_update_nav_menu_item($menu_id, $menu_item_id, array(
                        'menu-item-parent-id' => $parent_menu_item_id
                    ));
                }
            }
            
            error_log('Term ID: ' . $term_id);
                        
            $menu_items = wp_get_nav_menu_items($menu_id);
            foreach ($menu_items as $menu_item) {
                if ($menu_item->ID == $menu_item_id) {
                    error_log('Item Object ID: ' . print_r($menu_item, true));
                    break;
                }
            }
        }
    }
}

add_action('create_term', 'add_product_category_to_menu', 10, 3);

Error Log Output

[10-Jul-2024 09:44:50 UTC] Parent Menu ID: 1905
[10-Jul-2024 09:44:50 UTC] Term ID: 202
[10-Jul-2024 09:44:50 UTC] Item Object ID: WP_Post Object
(
    [ID] => 1911
    [post_author] => 1
    [post_date] => 2024-07-10 09:44:50
    [post_date_gmt] => 2024-07-10 09:44:50
    [post_content] => 
    [post_title] => 
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] => 
    [post_name] => 1911
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2024-07-10 09:44:50
    [post_modified_gmt] => 2024-07-10 09:44:50
    [post_content_filtered] => 
    [post_parent] => 196
    [guid] => https://dev.***/1911/
    [menu_order] => 62
    [post_type] => nav_menu_item
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
    [db_id] => 1911
    [menu_item_parent] => 1905
    [object_id] => 1911
    [object] => custom
    [type] => custom
    [type_label] => Individueller Link
    [title] => 
    [url] => 
    [target] => 
    [attr_title] => 
    [description] => 
    [classes] => Array
        (
            [0] => 
        )

    [xfn] => 
)

thanks for taking your time

I try to create a menu item for every new product category created, but it just creates an empty custom menu item (in the right place). If I however don't assign a menu-item-parent-id it works fine (but not in the right place obviously).

function add_product_category_to_menu($term_id, $tt_id, $taxonomy) {
    
    if ($taxonomy === 'product_cat') {
        
        $term = get_term($term_id, $taxonomy);
        $menu_name = 'main-menu';
        $menu = wp_get_nav_menu_object($menu_name);

        if ($menu) {
            $menu_id = $menu->term_id;
            $term_name = $term->name;

            function get_parent_menu_item_id_by_name($parent_id, $menu_id) {
                $parent_term = get_term($parent_id, 'product_cat');
                $menu_items = wp_get_nav_menu_items($menu_id);

                foreach ($menu_items as $menu_item) {
                    if (trim($menu_item->object_id) === trim($parent_term->term_id)) {
                        return $menu_item->ID;
                    }
                }
            }
            
            $menu_item_id = wp_update_nav_menu_item($menu_id, 0, array(
                'menu-item-title' => $term_name,
                'menu-item-object-id' => intval($term_id),
                'menu-item-object' => 'product_cat',
                'menu-item-type' => 'taxonomy',
                'url' => get_term_link($term),
                'menu-item-status' => 'publish'
            ));

            // If the category has a parent, update its parent ID
            if (!empty($term->parent) && $term->parent != 0) {
                $parent_term = get_term($term->parent, 'product_cat');
                
                if (!is_wp_error($parent_term)) {
                    $parent_menu_item_id = get_parent_menu_item_id_by_name($parent_term, $menu_id);
                    error_log('Parent Menu ID: ' . $parent_menu_item_id);
                    wp_update_nav_menu_item($menu_id, $menu_item_id, array(
                        'menu-item-parent-id' => $parent_menu_item_id
                    ));
                }
            }
            
            error_log('Term ID: ' . $term_id);
                        
            $menu_items = wp_get_nav_menu_items($menu_id);
            foreach ($menu_items as $menu_item) {
                if ($menu_item->ID == $menu_item_id) {
                    error_log('Item Object ID: ' . print_r($menu_item, true));
                    break;
                }
            }
        }
    }
}

add_action('create_term', 'add_product_category_to_menu', 10, 3);

Error Log Output

[10-Jul-2024 09:44:50 UTC] Parent Menu ID: 1905
[10-Jul-2024 09:44:50 UTC] Term ID: 202
[10-Jul-2024 09:44:50 UTC] Item Object ID: WP_Post Object
(
    [ID] => 1911
    [post_author] => 1
    [post_date] => 2024-07-10 09:44:50
    [post_date_gmt] => 2024-07-10 09:44:50
    [post_content] => 
    [post_title] => 
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] => 
    [post_name] => 1911
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2024-07-10 09:44:50
    [post_modified_gmt] => 2024-07-10 09:44:50
    [post_content_filtered] => 
    [post_parent] => 196
    [guid] => https://dev.***.com/1911/
    [menu_order] => 62
    [post_type] => nav_menu_item
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
    [db_id] => 1911
    [menu_item_parent] => 1905
    [object_id] => 1911
    [object] => custom
    [type] => custom
    [type_label] => Individueller Link
    [title] => 
    [url] => 
    [target] => 
    [attr_title] => 
    [description] => 
    [classes] => Array
        (
            [0] => 
        )

    [xfn] => 
)

thanks for taking your time

Share Improve this question asked Jul 10, 2024 at 9:54 WMMediaWMMedia 515 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Update

Solution was easy I'm stupid I just tweaked the menu creation and directly assigned the parent-menu-id:

if (!empty($term->parent) && $term->parent != 0) {
            $parent_menu_item_id = get_parent_menu_item_id_by_name($term->parent, $menu_id);
            if ($parent_menu_item_id) {
                $menu_item_id = wp_update_nav_menu_item($menu_id, 0, array(
                    'menu-item-title' => $term_name,
                    'menu-item-object-id' => intval($term_id),
                    'menu-item-object' => 'product_cat',
                    'menu-item-type' => 'taxonomy',
                    'url' => get_term_link($term),
                    'menu-item-parent-id' => $parent_menu_item_id,
                    'menu-item-status' => 'publish'
                ));
            } else {
                $menu_item_id = wp_update_nav_menu_item($menu_id, 0, array(
                    'menu-item-title' => $term_name,
                    'menu-item-object-id' => intval($term_id),
                    'menu-item-object' => 'product_cat',
                    'menu-item-type' => 'taxonomy',
                    'url' => get_term_link($term),
                    'menu-item-status' => 'publish'
                ));
            }
        }

本文标签: phpCan39t assign menu parent id or menu item breaks