In Genesis Facebook group, a user asks:
HELP > I need to reposition the Primary navigation ONLY in the blog pages.
Currently, the primary nav is after the header, and I need to put it before the header in the blog page (also in single post page, category page, archive page and search results page). I tried a function but I have the white screen, so it’s clearly poorly formed.
I’m using Genesis Sample.
I appreciate any suggestion.
We can place the reposition code inside a function hooked at a location that is immediately above the top most hook (contained inside the function) so an if conditional can be set.
While the tutorial has been written for Genesis Sample child theme it should work with minor adjustments in any Genesis child theme.
Adding the following in child theme’s functions.php will place the Primary Navigation above the header on single Posts, archives (incl. category pages) and search pages:
// Reposition the primary navigation menu conditionally | |
add_action( 'genesis_before', 'sk_resposition_primary_nav' ); | |
function sk_resposition_primary_nav() { | |
// if we are not on a single Post page or archive page or search results page, abort. | |
if ( ! ( is_singular( 'post' ) || is_archive() || is_search() ) ) { | |
return; | |
} | |
remove_action( 'genesis_after_header', 'genesis_do_nav' ); | |
add_action( 'genesis_before_header', 'genesis_do_nav' ); | |
} |
I’m a bit of a coding novice so I always read your posts with interest to learn from them. I’m wondering why you wrote the ‘if’ condition to give the abort return if page is not post/archive/search, rather than writing the opposite i.e. an if condition that says if is post/archive/search, then remove/add actions. Does the function have to have the abort option written into it? I thought if the ‘if’ condition is not satisfied, then the function would just do nothing anyway? i.e. it would leave the menu hooked below the header.
No.
It is okay and it will work if (pardon the pun) you write it ‘normally’ i.e., w/o the not.
But the reason why I prefer writing “if not” rather than “if” is because it’s recommended to exit early.
Remember: Return early and hook late.
Thanks. Tips like this best coding practice are one of the reasons why I love the content you post.