- This topic has 9 replies, 3 voices, and was last updated 3 years, 1 month ago by
David.
-
AuthorPosts
-
November 8, 2022 at 10:32 am #2406790
nvc
Hello everyone,
I am having some issues with making my site multilingual.
When I’ve first started it I made for all of the English speaking countries separate pages like that:
www. frontpage. com/usa
www. frontpage. com/uk
www. frontpage. com/canada
www. frontpage. com/new-zealand
www. frontpage. com/x-default
And I’ve added with the Hook Element to the wp_head the proper hreflang tag. Everything is working just fine and Google ranks the different pages for the specific country.
But now I want to add Spanish and German language and add language switcher with flags. I’ve installed some plugin but it changes my old hreflang tags and when I do hreflang check they are not working.
I am not able to do the other languages like I did with the English language because the menus need to be changed.
Do you have any easy solution to that?
Thank youNovember 8, 2022 at 2:11 pm #2407058Ying
StaffCustomer SupportHi there,
GP doesn’t have any functions in this field, so Unfortuanly there’s no easy solutions from GP.
I would recommend reaching out to the plugin’s support to see if they have any insights.
November 9, 2022 at 3:09 am #2407769nvc
Thanks for your answer Ying.
A question came to my mind when I was thinking how can I create multilingual site without plugin.
In GP there is an option I can set personal header and footer for particular pages. So i can create these Elements in other language for example. The question is if there is an option I can remove the menu from these pages and add custom menu only for these pages?
If yes I will be able to create full page with other language and add the proper hreflang tags. I guess that will work in my case.
What do you think?
RegardsNovember 9, 2022 at 5:06 am #2407936David
StaffCustomer SupportHi there,
if you create different language menus in Appearance > Menus.
You can then use a PHP Snippet to check the current URI for the subfolder eg./spainand then swap the menufunction db_switch_menus( $args ) { // Get the current URI for $locale $locale = $_SERVER['REQUEST_URI']; // apply logic to primary nav if( 'primary' === $args['theme_location'] ) { // check $locale and swap menu ID if ( strpos( $locale, '/spain') !== false ) { $args['menu'] = '55'; } if ( strpos( $locale, '/germany') !== false ) { $args['menu'] = '77'; } } return $args; } add_filter( 'wp_nav_menu_args', 'db_switch_menus' );Each
ifstatement eg.if ( strpos( $locale, '/spain') !== false ) { $args['menu'] = '55'; }Checks the URI for the appropriate sub folder eg.
/spainand if true it swaps the Menu ID to55
So you just need to match the subfolder name with the appropriate menu ID.November 29, 2022 at 9:07 am #2441428nvc
Thanks David for your answer but my coding skills are really bad. Would you please explain to me with more details. Thank you.
I already found a solution how to build multilingual using Parent and Child pages. The only issue left is the menu. Is there a way I can create a menu with Elements and include/exclude it to different pages/posts?November 30, 2022 at 3:43 am #2442489David
StaffCustomer SupportOK, there is no way in Elements to swap the menus that get displayed in the primary / secondary navs
Here i rewrite the code:
function db_switch_menus( $args ) { // Get the current URI for $locale $locale = $_SERVER['REQUEST_URI']; // apply logic to primary nav if( 'primary' === $args['theme_location'] ) { // check $locale and swap menu ID if ( strpos( $locale, '/spain') !== false ) { $args['menu'] = 'spain'; } if ( strpos( $locale, '/germany') !== false ) { $args['menu'] = 'germany'; } } return $args; } add_filter( 'wp_nav_menu_args', 'db_switch_menus' );If in your Appearance > Menus you create 3 x menus for example:
1. English language ( and the default menu ) you can name it whatever.
2. Spanish language and name the menu:spain
3. German language and name the menu:germanyAdding the above code will swap the menu to
spainif the user is in the/spaindirectory of the site.
And same for german.If thats not working let me know.
November 30, 2022 at 8:43 am #2443131nvc
Thanks David,
I will make a backup first and then I will try with you code. I guess that I need to add that code in the functions.php file, right?
RegardsDecember 1, 2022 at 4:09 am #2444251David
StaffCustomer SupportThe code is PHP.
This article explains how to add the code:https://docs.generatepress.com/article/adding-php/
TLDR: Are you using a Child Theme?
IF yes, then you add the PHP to your Child Themes functions.php
IF no, then use the Code Snippets plugin ( link the above doc ) and add it to a new Snippet.March 6, 2023 at 11:04 am #2557814nvc
David, i did it : ) Now i have different menus for my English and for my Spanish version.
I’ve done everything as you said and it works perfectly. Works sooo smooth : )
Awesome support as always. You’re the best guys!
Thank you!
RegardsMarch 7, 2023 at 3:05 am #2558476David
StaffCustomer SupportGlad to be of help
-
AuthorPosts
- You must be logged in to reply to this topic.