Add id to menu items in wp_nav_menu

Sounds like you would need to use a Custom Walker Function I’ve found some code on the WordPress Suport Forums which might be of some use: Walker Class – Pastebin class custom_nav_walker extends Walker_Nav_Menu { function start_el(&$output, $item, $depth, $args) { global $wp_query; $indent = ( $depth ) ? str_repeat( “\t”, $depth ) : ”; … Read more

WordPress get or set active menu class

This is relatively easily done with a filter. function alter_menu_class_wpse_113022($args) { // var_dump($args); // debug $args[‘menu_class’] = ‘my-nav-menu’; return $args; } add_filter(‘wp_nav_menu_args’,’alter_menu_class_wpse_113022′); That will make this line: <ul id=”menu-scroll” class=”nav-menu”> Look like this one: <ul id=”menu-scroll” class=”my-nav-menu”> But that could cause themes to break horribly. If a theme has used that class to apply styles … Read more

Check if menu id = $specific_id – then insert specific

Creating a custom Walker is a valid way to solve this, and is the first thing that crossed my mind, but it is not strictly necessary. It is possible to pull this off with filters, albeit by mildly abusing one of them 🙂 Proof of concept: function insert_image_wpse_130477($item_output) { remove_filter(‘walker_nav_menu_start_el’,’insert_image_wpse_130477′); $prepend = apply_filters(‘my_item_prepend’,”); return $prepend.$item_output; … Read more

Set the active Navigation Menu from a plugin

You can use the wp_nav_menu_args filter ( Codex reference ) to set a theme location to use a specific menu. Example: function test_wp_nav_menu_args( $args=”” ) { // only set menu for the primary navigation menu. if ( $args[‘theme_location’] != ‘primary’ ) { return $args; } // change {main-menu} to be the slug of the menu … Read more

Get top items number from wordpress menu

You can use the wp_get_nav_menu_object to retrieve the menu as an object, which you can subsequently establish the length of. Like this: $wpse140949_menu = wp_get_nav_menu_object( ‘your-menu-name-or-slug’ ); $tabcount = $wpse140949_menu->count; You can the use this on your call to wp_nav_menu: wp_nav_menu ( array ( ‘menu_class’ => ‘item-‘ . $tabcount, …. ));

Set if condition with wp_nav_menu

No need to use conditional here to hide your nav menu when it is empty. Have a look at wp_nav_menu( $args );. It accepts the following arguments $defaults = array( ‘theme_location’ => ”, ‘menu’ => ”, ‘container’ => ‘div’, ‘container_class’ => ”, ‘container_id’ => ”, ‘menu_class’ => ‘menu’, ‘menu_id’ => ”, ‘echo’ => true, ‘fallback_cb’ … Read more

Add link at the end of wp_nav_menu

This is what worked for me (based on this).. hope it will be helpful for others too.. add_filter( ‘wp_nav_menu_items’,’primary_navigation’, 10, 2 ); function primary_navigation( $items, $args ) { if( $args->theme_location == ‘primary_navigation’ ) { if ( ! isset( $_SESSION[‘menu_country’] ) ) { $country = json_decode( file_get_contents( “http://freegeoip.net/json/” . esc_url( $_SERVER[‘REMOTE_ADDR’] ) ) ); $_SESSION[‘menu_country’] = … Read more

Hide menu item in certain condition

In your actual code the function to add the script is never executed. You can put the function add_my_script() wherever you want but you need to hook it into a WordPress event. The best event for this case is the wp_enqueue_scripts: add_action(‘wp_enqueue_scripts’, ‘add_my_script’); function add_my_script() { wp_enqueue_script( ‘hide-menu’, // name your script so that you … Read more

Run a filter when a walker runs

If all of it is your own code then it would be reasonable to just implement that as part of walker. If you need to add a filter while third party walker runs you could probably: Add it at wp_nav_menu_args hook, checking if walker is set to that: add_filter( ‘wp_nav_menu_args’, function( $args ) { if … Read more