• With earlier versions of default WordPress themes I was able to display an alternative main menu depending on the category of the given page or post, using this filter in my functions.php:

    ( In this example we have German and French categories that display menus named German or French. All other categories display the menu named English. )

    function my_conditional_menus( $args = '' ) {
      if (in_category('german')) { 
        $args['menu'] = 'german';
      } elseif (in_category('french')) {
        $args['menu'] = 'french';
      } else {
        $args['menu'] = 'english';
      }
      return $args;
    }
    add_filter( 'wp_nav_menu_args', 'my_conditional_menus' );

    Unfortunately I noticed this solution does not work with the Twenty Twenty-Two theme.

    How could I make it useable for this theme?

Viewing 15 replies - 1 through 15 (of 26 total)
  • Did you already find a solution? I have a make a multi language site and i also want to show different menu’s on the right language..

    Thread Starter akopacsi

    (@akopacsi)

    Unfortunately I didn’t not. I’m also running the same question on Stackexchange without any luck.

    Hi there. The issue here is a limitation in the way that the block editor handles navigation for full site editing.

    You may have luck pursuing the “Legacy Menu” solution noted in this thread: https://github.com/WordPress/gutenberg/issues/36576

    If not, I suggest opening an issue in the Gutenberg repository. The problem you’re experiencing would be the same for all block themes, not just Twenty Twenty-Two. Since that’s the case, the best fix would be upstream in the Gutenberg repo:

    https://github.com/WordPress/gutenberg/issues

    I hope that’s helpful!

    Thread Starter akopacsi

    (@akopacsi)

    Thank you @kjellr

    I am facing the same issue, and after searching a while without success, the light bulb went on and I searched for “conditional blocks” instead of “conditional menu.” Theoretically, at least, if you have multiple navigation blocks and hide all but one depending on some condition, you get the same results as with conditionally-displayed items on a single menu.

    Toward that end, I am looking at a plugin called (surprise!) “Conditional Blocks.” If anyone has used it or comes up with a better solution, I would like to know.

    Update:

    I installed “Block Visibility” instead of “Conditional Blocks” for the simple reason that I have taken a webinar about full site editing by the author of the former plugin. I expect the latter would also work.

    I created two Navigation blocks and used the plugin to show or hide based on log-in status. In the settings for the plugin, I enabled “Full Control Mode”; I think that was needed in order to see the “Visibility” options for the navigation block (but maybe I just overlooked that area in the block settings the first time).

    Seems to work just fine. Yay!

    • This reply was modified 2 years, 3 months ago by greentreefrog.
    Thread Starter akopacsi

    (@akopacsi)

    @greentreefrog Thank you for the idea. I will check it out. It sounds like a good solution if nothing else works. But the best option would be solving this without a 3rd party plugin.

    Hi @akopacsi and @greentreefrog ,

    I was wondering if you’ve found a solution for this?

    I am using the twenty twenty-two theme and trying to display a different header (including a navigation menu in a different language), based on the URL path.

    This is because on my site, all languages are distinguishable by subdirectories, like so:

    https://www.domain.com/de/postname
    https://www.domain.com/es/postname

    So I want to display a site header with a German nav menu if the URL contains “/de/”

    I was not able to find any free solutions for this, could you maybe point me in the right direction how to solve this?

    Thank you!

    Thread Starter akopacsi

    (@akopacsi)

    Unfortunately I couldn’t find any solution. I switched back to 2021 theme and I use the my_conditional_menus function I showed in my initial post.

    I also find the new way of managing menu item pretty awful. I prefer the old fashioned way.

    For the Rye website we developed we also required a conditional menu system for when users were signed in.

    We chose to ditch the Navigation block in favour of a menu plugin (one that we develop but there are plenty that do a similar job) which uses the conventional / legacy Menus. We injected the menu into the header by using a Shortcode Block.

    Oliver

    Thanks for replying so quickly, guys, much appreciated!

    @akopacsi : yes, I was actually also considering switching back to 2021.

    However, this would cause other problems and lots of work as some functionality currently provided by the full site editing would be lost (like different post templates with specific layout and content blocks).

    @domainsupport using the legacy menu sounds like an interesting option to pursue!

    Would you be able to name a few of the plugins you know that might do the job?

    You said there were plenty, but when looking for “legacy menu plugin”, I’m not actually finding anything?

    Thanks a lot!

    All you need is a menu / navigation plugin with a shortcode. Try this search (I added “FSE” for good measure).

    I am using the twenty twenty-two theme and trying to display a different header (including a navigation menu in a different language), based on the URL path.

    Hi @rhisflow alternatively with the site editor, you can create a custom template that has a different header part. When you create the template you will see an option to edit the content inside the header, this will give you the option to change the menu items and links for this particular template.

    You can then assign this template to the pages you pick.

    I am not sure if I understand this correctly – is it that you need the menu to change language depending on the language of the site? I create sites in English and French and use translatepress, which translates the menu items.

    Here is an example with 2022 theme (translation in progress, only the home page has been translated so far.) The only issue I have not been able to solve so far is placing the EN and FR links into the navigation. But users can switch using the floating language switcher at the top right of the page.

    Hi @thelmachido , thanks for your suggestion!

    However, I don’t think that actually works, at least I never got it working, which is why I was looking for a solution.

    With the new FSE, it seems you can only select one nav menu per header per template, and even if you create a different header in a different template, there’s no way you can implement a conditional logic to determine when the other template should be used (which would require a logic based on the URL path like “if it contains “/en/”, then use this template, etc).

    @tracyrosen , I also went down that path with Translatepress to try it and while it can indeed work if you only need to literally translate a single navigation menu, it does not work if you need different menu items in the other languages, which is what I needed.

    In other words, you can’t add or remove menu items that way for other languages.

    @domainsupport , thanks so much for your recommendation and pointing me in the right direction ?? !

    I ended up using the Max Mega Menu and after a few hours of setting everything up, it works quite nicely, so I consider this a good solution, thank you ?? !

    • This reply was modified 1 year, 9 months ago by rhisflow.
Viewing 15 replies - 1 through 15 (of 26 total)
  • The topic ‘How to display conditional menus with Twenty-twenty two theme?’ is closed to new replies.