Adding language switcher to main nav

Community Forums Forums Design Tips and Tricks Adding language switcher to main nav

This topic is: resolved

Tagged: 

This topic contains 15 replies, has 3 voices, and was last updated by  lucaslem 7 months, 4 weeks ago.

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #58338

    lucaslem
    Participant
    Post count: 73

    Hello, I am using the Multisite Language Switcher to link two different language sites (multisite install). I would like to add the switcher widget (essentially just a link) inside the primary navigation, so that it might sit along-side my other menu items. Just to be clear, I don;t just want it in the same widget area (currently header right) I would like to incorporate it into menu-primary-navigation.

    Someone had a suggestion for a thesis theme here. Looking for similar solution.

    Many Thanks

    #58533

    David Chu
    Participant
    Post count: 1187

    Hi,
    I searched for quite awhile to find a way to do something like that. I even followed Bill Erickson’s very fancy tutorial about some sort of fancy menu mod. I couldn’t get what I was looking for.

    Instead, I figured out a way to do it using a more stock WordPress solution. I filtered the Primary menu, adding some code to it, and just did nothing if it wasn’t the Primary menu. As you can see, I put my HTML block just before the menu. It could go after it, too. So this procedure should give you an idea of how it could be done with your multilanguage thingie. You may need to get your hands a little dirty with PHP, and use your plugin’s directions for placing the switcher into your theme code.

    // add block of text to menu
    add_filter('wp_nav_menu', 'dc_add_nav_text', 10, 2);
    function dc_add_nav_text ( $menu, $args ) {
         $args = (array)$args;
         if ( 'primary' !== $args['theme_location'] ) {
              return $menu;
         }
    
         $text = '<div id="navExtra">Creative ways to grow your business</div>';
         return $text . $menu;
    }
    

    Good luck, Dave


    David Chu · Custom WordPress Developer – likes collaborating with Designers

    #58611

    lucaslem
    Participant
    Post count: 73

    Hi Dave,

    Pretty new to backend dev (PHP) so it’s all slow going and not always clear to me where I can use genesis-specific code and where I need to incorporate general wordpress code.

    I can across this link which includes some of your code. I’ll poke around all of this and give feedback as to the failure or success in what I am trying to achieve.

    In the mean time if anyone else has a Genesis-specific approach to this, I’m all ears.

    #58848

    David Chu
    Participant
    Post count: 1187

    Lucas,
    Sounds like a plan. I tend to look for Genesis solutions, too, because its hooks and filters are so handy, and generally require a small amount of code to get working. In some cases, the Genesis code becomes like shorthand – for instance, there’s Genesis code for making new widgets, but the stock WordPress code for that is not too much bigger. I think you could define some Genesis functions as convenient wrappers for WordPress ones, while others are delightfully unique.

    The menu area is a little tricky, because it is a complex part of the system. If I remember, the Bill Erickson code I looked at was for modifying each menu item rather than adding a chunk of code next to it, so to speak.

    I actually answered someone on Twitter one time who was looking to do something like what you are asking. She never responded, so I have no idea how far she got. I’ll look forward to seeing what you come up with.

    This is a fairly advanced topic, so it does require some careful study. If and when you get some code working, some CSS work will be needed to make it all look good. I say this because I see Genesis newbies trying similar things without getting help, and frankly, they will have a tough time.

    One last thought… there is code in Genesis called Primary Navigation Additions. This adds various items to the menu, and examining that code could give ideas for this sort of thing, too. Doing that requires heavy geekiness, of course. :-)

    Dave


    David Chu · Custom WordPress Developer – likes collaborating with Designers

    #58908

    lucaslem
    Participant
    Post count: 73

    Ok, I see where my problem lies. I am misleading by referring to my “primary navigation” which, in the studiopress demo is the full-width nav (located after the genesis header) and for which your code works perfectly. However, in my case I wanted the primary nav to be aligned with the logo and so I chose to place it in the header right widget area (genesis_header_right). In other words, there is no primary for theme_location
    Will keep digging. Perhaps there is a better way for me to structure my nav using CSS rather than using the header widget area.

    #58912

    lucaslem
    Participant
    Post count: 73

    Or (even better) is there a way of targeting the nav in the header-right widget either by ID or theme location?

    #59034

    David Chu
    Participant
    Post count: 1187

    You bet! For details, go here.
    Adapting the code above, you can specify which menu, instead of which menu location. You’ll get the idea, I’m sure.

         if ( 'Cool Lucas Side Menu' !== $args['menu'] ) {
              return $menu;
         }
    

    Dave


    David Chu · Custom WordPress Developer – likes collaborating with Designers

    #59259

    lucaslem
    Participant
    Post count: 73

    Hmmmm, I love this proposed solution to target by menu name rather than theme_location, but it doesn’t seem to be working. The name of my navigation (from admin panel) is Primary Navigation. So here is the code changed accordingly (using your block of text example as a test):

    // add block of text to menu
    add_filter('wp_nav_menu', 'dc_add_nav_text', 10, 2);
    function dc_add_nav_text ( $menu, $args ) {
         $args = (array)$args;
         if ( 'Primary Navigation' !== $args['menu'] ) {
         return $menu;
    }
     
         $text = '<div id="navExtra">Creative ways to grow your business</div>';
         return $text . $menu;
    }

    Something just isn’t right…? Sorry for dragging this out, but I really can’t figure out where the issue lies and google is not helping.

    #59261

    lucaslem
    Participant
    Post count: 73

    Aaahhhhh, just saw my syntax error!

    Needed to change
    if ( 'Primary Navigation' !== $args['menu'] )
    to
    if ( 'Primary Navigation' == $args['menu'] )

    Going crossed-eyed!

    #59266

    lucaslem
    Participant
    Post count: 73

    Nope, that just adds it everywhere. Ugh.

    #59270

    Sridhar Katakam
    Participant
    Post count: 851

    Another similar method (not that it is better, just stating options) is to create a sidebar (widgeted area) and stick this in the navigation. Then you can put any widget in this sidebar.

    See this for an example: http://sridharkatakam.com/how-to-add-a-widgeted-area-in-navigation-bar-in-genesis/


    #59273

    David Chu
    Participant
    Post count: 1187

    Look at your menus. Primary Navigation is a location, not a name of the menu. Look at the menu itself for the name. This is my final answer! :-) The rest is up to you.

    Good luck.


    David Chu · Custom WordPress Developer – likes collaborating with Designers

    #59274

    lucaslem
    Participant
    Post count: 73

    David, Primary Navigation just happens to be what I called my main nav in the admin; in this case it is the name of the menu. I have also tried it with other menus registered on the site (one called “minor nav” etc). Still no dice. Thanks for your patience and final answer. Will keep searching.

    Thanks Sridahr, will explore this avenue as well though at first glance it’s not quite what I’m after.

    #59315

    lucaslem
    Participant
    Post count: 73

    For anyone interested I got a solution to the issue (largely came from support from the language swithcer plugin developer):

    wp_nav_menu_items is a filter-hook. You should check what is in $items and $args. Why not just check the name of the handle?
    Read on here: http://wordpress.stackexchange.com/questions/2143/customizing-only-a-specific-menu-using-the-wp-nav-menu-items-hook

    This does not explain to me why the last code block I posted here does not work, but it did fix my issue.

    #59316

    David Chu
    Participant
    Post count: 1187

    Hi,
    One last clarifying point: in my first code example, the exclamation point means “NOT”. So the code is organized like this, using pseudo-code:

    —————————————————
    If the menu is NOT the “one I have in mind”, get out, and just show the regular menu.

    If it is the “one I have in mind”, stick some gunk right next to it, and then get out.
    ——————————————————

    It doesn’t have to be organized like that. You could simply code it to look affirmatively for that menu, and when found, do something. Also, to get this type of thing, you’ll probably need to read up on arrays and subscripting. :-) I know that’s not welcome news for everyone. Even though Genesis is absolutely killer, to make it do every last little thing, some PHP ability is mandatory, IMO.

    Sridhar’s idea is an interesting one. It allows more flexibility than my hard-coded method.

    D


    David Chu · Custom WordPress Developer – likes collaborating with Designers

Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic.