[Resolved] Conditional tags in code snippets

Home Forums Support [Resolved] Conditional tags in code snippets

Home Forums Support Conditional tags in code snippets

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #314658
    Minh

    Hi GP team,

    I was trying to add conditional tags in code snippets but it fails. Like below code: (I wanted the primary navigation appears below the page header ONLY in home page (which is a static page) ):

    if ( is_front_page() ) :
    add_action('after_setup_theme','generate_change_nav_position');
    function generate_change_nav_position()
    {
          remove_action( 'generate_after_header', 'generate_add_navigation_after_header', 5 );
          add_action( 'generate_after_header', 'generate_add_navigation_after_header', 29 );
    }
    endif;

    Am I doing wrong?

    Thanks.

    #314659
    Tom
    Lead Developer
    Lead Developer

    Almost!

    Try this:

    add_action('after_setup_theme','generate_change_nav_position');
    function generate_change_nav_position()
    {
        if ( is_front_page() ) :
            remove_action( 'generate_after_header', 'generate_add_navigation_after_header', 5 );
            add_action( 'generate_after_header', 'generate_add_navigation_after_header', 29 );
        endif;
    }
    #314665
    Minh

    Hi Tom,

    Thank you. Unfortunately it is not how I want. The navigation now is below the logo but not below the entire page header (My home page has an big image in page header).

    Fyi: Other than the home page, I want the main navigation to FLOAT right in the rest of the website (with the logo is in the left).

    How can I do that?

    Sorry for my lack of coding knowledge. Thank you very much Tom!

    #314744
    Tom
    Lead Developer
    Lead Developer

    Can you link me to your site?

    To adjust the navigation location per page you would use this filter: https://docs.generatepress.com/article/navigation-location/#generate_navigation_location

    #316260
    Minh

    Hi Tom,
    Sorry for the late reply as I was uploading it to a live host. Have a look at this: http://dacsancamau.vn/
    Best,
    Minh.

    #316342
    Tom
    Lead Developer
    Lead Developer

    Can you try changing your navigation location to after the header in Customize > Layout > Primary Navigation?

    #316575
    Minh

    Hi Tom,

    It’s not working. It’s still inside the page header.

    The point is: I want the main navigation to sit below the page header (only in the home page), and float right in the rest of the website. So I have to make the main navigation float right by default, and then insert your code with WP conditional tag so that the navigation can sit below the page header in the home page (and the site logo then floats in the center of the header, which I can manage to do).

    Thank you Tom.

    #316605
    Tom
    Lead Developer
    Lead Developer

    This would be your code then:

    add_action('wp','generate_change_nav_position');
    function generate_change_nav_position()
    {
        if ( is_front_page() ) :
            remove_action( 'generate_after_header_content', 'generate_add_navigation_float_right', 5 );
            add_action( 'generate_after_header', 'generate_add_navigation_float_right', 29 );
        endif;
    }
    #316612
    Minh

    Not working, Tom. ๐Ÿ™

    #316613
    Tom
    Lead Developer
    Lead Developer

    Just made another adjustment, try the new code.

    #316616
    Minh

    Still floating right, Tom.

    #316618
    Tom
    Lead Developer
    Lead Developer

    How are you adding the PHP? Technically that shouldn’t be possible.

    I just made another tweak, worth a shot.

    #316698
    Minh

    That works! Thank you very much Tom.

    #316779
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

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