[Resolved] Modify item in secondary menu through php

Home Forums Support Modify item in secondary menu through php

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #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;

    #1037731
    Tom
    Lead Developer
    Lead Developer

    Hi 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?

    #1038229
    Terry

    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.

    #1038556
    Tom
    Lead Developer
    Lead Developer

    So 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-menu

    Then, 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;
    } );
    #1041736
    Terry

    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.

    #1041835
    Tom
    Lead Developer
    Lead Developer

    Sorry about that – edited.

    Glad it worked! 🙂

    #1266644
    Terry

    Closing this.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.