Archived topic
Conditional tags in code snippets
13 replies · Started by Minh on May 6, 2017
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.
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;
}
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!
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
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.
Can you try changing your navigation location to after the header in Customize > Layout > Primary Navigation?
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.
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;
}
Not working, Tom. :(
Just made another adjustment, try the new code.
Still floating right, Tom.
How are you adding the PHP? Technically that shouldn't be possible.
I just made another tweak, worth a shot.
That works! Thank you very much Tom.
You're welcome :)