Hey! I have added a wp_loginout() to my header using a snippet in my functions.php:
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
ob_start();
wp_loginout('index.php');
$loginoutlink = ob_get_contents();
ob_end_clean();
$items .= '<li>'. $loginoutlink .'</li>';
return $items;
}
The thing is that it shows the login link in every one of my three menues:
function register_main_menus() {
register_nav_menus(
array(
'primary-menu' => __( 'Primary Menu' ),
'secondary-menu' => __( 'Secondary Menu' ),
'footer-menu' => __( 'Footer Menu' ),
)
);
};
I would like to target the wp_nav_menu_items filter to only include the login link in the primary menu. Ideas? Thanks in advance
Hey! I have added a wp_loginout() to my header using a snippet in my functions.php:
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
ob_start();
wp_loginout('index.php');
$loginoutlink = ob_get_contents();
ob_end_clean();
$items .= '<li>'. $loginoutlink .'</li>';
return $items;
}
The thing is that it shows the login link in every one of my three menues:
function register_main_menus() {
register_nav_menus(
array(
'primary-menu' => __( 'Primary Menu' ),
'secondary-menu' => __( 'Secondary Menu' ),
'footer-menu' => __( 'Footer Menu' ),
)
);
};
I would like to target the wp_nav_menu_items filter to only include the login link in the primary menu. Ideas? Thanks in advance
Share Improve this question asked Mar 13, 2011 at 11:36 user3901user39013 Answers
Reset to default 9See this related question. I guess in your case you'd wrap your code in if( $args->theme_location == 'primary-menu' )
.
Please modify your code with the following code
You forgot to add condition if ($args->theme_location == 'primary-menu')
this condition check if menu is primary menu or not.
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
if ($args->theme_location == 'primary-menu') {
ob_start();
wp_loginout('index.php');
$loginoutlink = ob_get_contents();
ob_end_clean();
$items .= '<li>' . $loginoutlink . '</li>';
return $items;
}
}
function register_main_menus() {
register_nav_menus(
array(
'primary-menu' => __( 'Primary Menu' ),
'secondary-menu' => __( 'Secondary Menu' ),
'footer-menu' => __( 'Footer Menu' ),
)
);
};
Actually, I am fairly new to WordPress development, and am trying to fully understand the documentation. I find that there are so many ways to skin a cat, and some are a lot more complicated than others. in the case of the above, the probem is solved much easier than previous described;
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
the wp_nav_menu_items hook is actually dynamic and you can simply add in between wp_nav_menu__items to target the specific menu. example;
add_filter('wp_nav_primary-menu_menu_items', 'add_login_logout_link', 10, 2);
if you inspect the wp-includes/nav-menu-template.php you can verify this fact.