I have created multiple different headers as templates with Elementor.
I would like to display all of the different headers based on user role (Logged in/Logged out) and page.
I’m looking for a code snippet that I could easily customize to assign all of the different headers for different scenarios.
Could someone please create an example code snippet that would:
- Display header A for Logged Out users on the entire
website, EXCEPT pages X and Y. - Display header B for Logged In users on the entire
website, EXCEPT pages X and Y. - Display header C for Logged Out users only on pages X and
Y. - Display header D for Logged In users only on pages X and
Y.
This way, people can easily copy the code and customize it to fit their needs.
EDIT
There’s 2 places where I can create templates.
1st one is added by Elementor and is found in Admin > Templates > Saved Templates. Here I can create either section or page templates (Screenshot).
2nd one is added by my theme, OceanWP, and is found in Admin > Theme Panel > My Library. Here I can create only 1 type of template. The templates created here can later be assigned as custom headers or footers to individual pages or the entire website.
Are the templates created in these 2 places considered to be template parts? Is there a difference where I choose to create the header templates?
Here’s a list of the header templates I have created:
Template title | Post ID | |
---|---|---|
A | Main Header (Logged Out) | 5448 |
B | Main Header (Logged In) | 6714 |
C | Checkout Header (Logged Out) | 6724 |
D | Checkout Header (Logged In) | 3960 |
Here’s the page I want to have a different header than the entire website:
Page title | Post ID | Slug | |
---|---|---|---|
X | Checkout | 18 | checkout |
2
Answers
Something like this should work:
The following offer the same result as the previous answer but is minified and has less repetitiveness.
Following your comments
I’m guessing that what you refer as…
…are in fact templates part. Instead of using
get_header( string $name );
you would then useget_template_part( string $slug, string $name = null );
.$slug
and$name
can be anything that you chose.For example,
section-A.php
would beget_template_part( 'section', 'A' );
.In regards to specifying pages and templates.
is_page()
can take IDs, slugs or titles.$page
(int|string|int[]|string[])
(Optional) Page ID, title, slug, or array of such to check against. Default value: ”But you could also use other
is_
function likeis_search()
oris_archives()
,is_404()
… etc.Here is a complete list @ https://codex.wordpress.org/Conditional_Tags
If you want to add multiple conditional statement you can just add
elseif
statements in-between.If you want to get a better understanding of PHP operators, which are how conditional statements are built, take a look @ https://www.w3schools.com/php/php_operators.asp