Reputation: 542
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:
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 |
Upvotes: 0
Views: 1650
Reputation: 5449
The following offer the same result as the previous answer but is minified and has less repetitiveness.
<?php
if ( is_page( [ 'page-x', 'page-y' ] ) )
if ( is_user_logged_in() )
get_header( 'B' ); //... header-B.php
else
get_header( 'A' ); //... header-A.php
else
if ( is_user_logged_in() )
get_header( 'D' ); //... header-D.php
else
get_header( 'C' ); //... header-C.php
?>
I'm guessing that what you refer as...
section templates
...are in fact templates part. Instead of using get_header( string $name );
you would then use get_template_part( string $slug, string $name = null );
.
$slug
and $name
can be anything that you chose.
For example, section-A.php
would be get_template_part( 'section', 'A' );
.
<?php
//...
if ( is_user_logged_in() )
get_template_part( 'section', 'B' ); //... section-B.php
else
get_template_part( 'section', 'A' ); //... section-A.php
?>
In regards to specifying pages and templates. is_page()
can take IDs, slugs or titles.
is_page( int|string|int[]|string[] $page = '' )
Parameter | Description |
---|---|
$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 like is_search()
or is_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.
<?php
if ( is_page( [ 'page-x', 'page-y' ] ) )
//...
elseif ( is_search() || is_archive() )
//...
else
//...
?>
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
Upvotes: 1
Reputation: 142
Something like this should work:
<?php
if (! is_user_logged_in() && ! is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header A
}
if (is_user_logged_in() && ! is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header B
}
if (! is_user_logged_in() && is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header C
}
if (is_user_logged_in() && is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header D
}
?>
Upvotes: 2