Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to add a conditional menu item such as "/at" or "/de" or "/ch" in the wp_nav_menu. Since the link is conditional (based on the country the website is being viewed), it can't be added from the backend but instead has to be hardcoded.

How do I add this new conditional link at the end of what the wp_nav_menu is populating? Thank you.

share|improve this question
    
I see this is what I want.. wpbeginner.com/wp-themes/… –  Sushil May 7 at 6:48
add comment

1 Answer 1

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/".$_SERVER['REMOTE_ADDR']));
            $_SESSION['menu_country'] = strtolower($country->country_code);   
        }

       $items .=  '<li class="menu-produkte"><a href="/'.$_SESSION['menu_country'].'/shop">Produkte</a></li>';
    }
    return $items;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.