How can I add a sub menu to an existing navigation menu in WordPress?

I got the answer, here it is: I want to add submenus ffrom database to the menu named ‘Products’ Create a custom plugin and install it through Admin panel. Inside the functions.php write this code. This is upgrade safe way and will not brake if the theme is updated. add_filter( ‘wp_nav_menu_objects’, ‘ravs_add_menu_parent_class’ ); function ravs_add_menu_parent_class( … Read more

Hide menu items for visitors and filter based on role

You need to define something like this in your header.php file. <?php if ( is_user_logged_in() ) { echo ‘Welcome, registered user!’; } else { echo ‘Welcome, visitor!’; } ?> Create a new role with the following code and put it in your functions.php add_role(‘partners’, ‘Partners’, array( ‘read’ => true, // True allows that capability, False … Read more

How to Create a Custom WordPress Navigation Menu with the Code?

Well first of all you can use the menu feature in the newer WordPress. First you want to activate the fact that you want to use this feature via your functions.php file. // This theme uses wp_nav_menu() in two locations. register_nav_menus( array( ‘primary’ => __( ‘Primary Navigation’, ‘your_theme_name’ ), ‘secondary’ => __(‘Secondary Navigation’, ‘your_theme_name’) ) … Read more

Nav Menu and display post count

Yep, I’ve got the solution to this problem. You can retrieve number of child items right from menu_items retrieved by wp_get_nav_menu_items. This function retrieves a bunch of post objects containing info about ‘menu_item_parent’. Inside start_el function of Walker_Nav_Menu class, you can try the match agains $item->ID. This way: $locations = get_nav_menu_locations(); $menu = wp_get_nav_menu_object( $locations[‘primary’] … Read more

Replace menu tag with tag

It looks like the above can be achieved with the walker_nav_menu_start_el filter (see reference). <?php function edit_menu_item($item_output, $item) { if ( get_field( ‘dropdown’, $item) == ‘yes’ ) { return ‘<button>’.$item->title.'</button>’; } return $item_output; } add_filter(‘walker_nav_menu_start_el’,’edit_menu_item’, 10, 2); Credit goes to @s_ha_dum via this answer.