wp_nav_menu() – header menu – before after not working – fix(ish)
I thought I’d post this to help folk, as I needed this to work.
If you’re using a theme that uses wp_nav_menu() in the header.php and you want the before/after arguments to work.
Basically they don’t work because the menu falls back to wp_page_menu() as it is not a registered menu, and wp_page_menu() doesn’t contain the arguments ‘before’ and ‘after’ but if you don’t want to go through the process of registering the menu and creating it in Appearance > Menu etc. Which is a bit of faff.
Simply add the arguments to wp_page_menu() found in wp-includes/post-template.php
You will need to first add the argument to the $defaults i.e.:
$defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => '', 'after' => '');
Then add the args to the outputs for if is_front_page and for $output
$menu .= '<li ' . $class . '>'. $args['after'] . '<a href="' . home_url( '/' ) . '" title="' . esc_attr($text) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a>';
Then in your wp_nav_menu() in header.php add the argument e.g.
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'after' => '<span class=\'arrowdown\'> </span>' ) ); ?>
Then you should be good to go.
- The topic ‘wp_nav_menu() – header menu – before after not working – fix(ish)’ is closed to new replies.