- This topic has 6 replies, 2 voices, and was last updated 5 years, 10 months ago by
Terry.
-
AuthorPosts
-
October 17, 2019 at 1:38 pm #1037547
Terry
I have a page, virginiaavp.org, that translates between English and Spanish, using the Divi theme. If you go there, you’ll see that if you click on Espanol? it switches to English? I want to do the same thing on a new GP site. I’m using the following php to make the switch. Can I do the same thing by using a GP hook?
Note the use of $et_secondary_nav. That would obviously have to be a different variable, as it’s a Divi variable.
`if ( is_page( ‘home-page-espanol’ ) || is_page( ‘spanish-page’ ) || is_page( ‘spanish-version’ )) :
$lang = ‘spanish’;
elseif ( is_front_page() || is_page( ‘english-page’ ) || is_page( ‘english-version’ )) :
$lang = ‘english’;
else :
$lang = $_GET[‘lang’];
if ( is_null($lang)) :
$lang = ‘english’;
endif;
endif;if ( $lang == ‘english’ ) :
$et_secondary_nav = wp_nav_menu( array(
‘theme_location’ => ‘secondary-menu’,
‘container’ => ”,
‘fallback_cb’ => ”,
‘menu_id’ => ‘et-secondary-nav’,
‘echo’ => false,
) );
else :
$et_secondary_nav = wp_nav_menu( array(
‘theme_location’ => ‘espanol-top’,
‘container’ => ”,
‘fallback_cb’ => ”,
‘menu_id’ => ‘et-secondary-nav’,
‘echo’ => false,
) );
endif;October 17, 2019 at 8:02 pm #1037731Tom
Lead DeveloperLead DeveloperHi there,
Where is that code added in the Divi theme?
Are you wanting to use a function instead of a plugin like Polylang or WPML?
October 18, 2019 at 7:56 am #1038229Terry
It’s in the header. Basically, I have two versions of the primary and secondary menus, one each in English and one in Spanish. When they click on the Espanol button, both menus switch to Spanish, and vice versa for English. For each page, I’ve added custom fields with Spanish versions of the text. The users prefer seeing each language on the same page instead of having two different pages. That part is working fine, as you can see by browsing virginiaavp.org. So, I don’t want a plugin like Polylang doing the translation–the users are doing it on each page. What I need help with is in header.php, inserting the php to switch menus. I have separate module, in a page template, that keeps track of whether the current page is English or Spanish. It works fine with Divi, as that’s the theme I was using when I developed virginiaavp, but now I’m using GP and Gutenberg, as I find I can do anything Divi was offering with this combination.
October 18, 2019 at 7:43 pm #1038556Tom
Lead DeveloperLead DeveloperSo how are you adding this to GP? Using a Hook Element? Looking at the code, it should work in GP just like it does in Divi.
Personally, I would create a menu and give it a location:
secondary-menuThen, I would filter it and change the location depending on my conditions:
add_filter( 'wp_nav_menu_args', function( $args ) { if ( 'secondary-menu' === $args['theme_location'] && ( is_page( 'home-page-espanol' ) || is_page( 'spanish-page' ) || is_page( 'spanish-version' ) ) ) { $args['theme_location'] = espanol-top; } return $args; } );October 22, 2019 at 1:23 pm #1041736Terry
This worked great, and is much easier to implement. You did have a couple of typos, mainly another right paren after is_page( ‘spanish-version’ ), quotes around ‘espanol-top,’ and a return $args at the bottom, but I’m not complaining. Great job–thanks.
October 22, 2019 at 6:11 pm #1041835Tom
Lead DeveloperLead DeveloperSorry about that – edited.
Glad it worked! 🙂
May 2, 2020 at 12:02 pm #1266644Terry
Closing this.
-
AuthorPosts
- You must be logged in to reply to this topic.