I would like to change to homepage of a wordpress website depending on the role of the user who is logged in.
For the simple case ( i.e. whether a user is logged in or not ) I have tried using the function is_user_logged_in()
the code is as below:
if(!is_user_logged_in()){
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile;
}else{
echo "you are logged in";
}
But the problem is that it changes the content in the each and every page. I would like to do that for a particular page only.. ( i.e. homepage ). How do i do that? Any help would be greatly appreciated.
To do it you would have to add another condition(s), e.g. for the homepage:
if(!is_user_logged_in() && is_home()){
...
}
And similarly for the other type of the content: is_page(), is_category(), is_author(), etc. Check out the docs here.
check for the Id of the page and compare it to your wanted specific page
also you could set a category for the pages you want to alter and check for that
Related
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
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
}
?>
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
?>
Following your comments
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.
Source # https://developer.wordpress.org/reference/functions/get_template_part/
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: ''
Source # https://developer.wordpress.org/reference/functions/is_page/
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
I am currently using
<?php if ( is_home() ) :
get_header( 'home' );
endif;
?>
To call 'header-home.php' on the homepage only.
I also want to call 'header-fullwidth.php' on our full width posts.
This is one of our full width posts: http://www.sickchirpse.com/photos-vatnajokull-glacier/
This is a normal post (it has a sidebar): http://www.sickchirpse.com/how-survive-european-squat-house-berlin/
What can I add to achieve this?
if(get_page_template() == 'your-full-width-template'):
get_header( 'full-width' );
endif;
You can alwasy check the current page template path with the get_page_template() function.
I'm a bit new to this, but I researched this topic a bit online and can't seem to figure out what I'm doing wrong. So basically, I created a duplicate footer to call for the homepage. We use a marketing automation tool called Pardot and we don't want to track visits to the homepage. So I created a file in the footer folder next to "footer-default.php" called "footer-nopardot.php" that omits the code.
In the home.php file, I edit the bottom with
<?php
get_footer('nopardot'); ?>
But it appears the homepage is still calling the default footer. Any advice on what to do or what I'm doing wrong?
Thanks!
just add this to your home template:
<?php
if ( is_home() ) :
get_footer( 'nopardot' );
else :
get_footer();
endif;
?>
you can use this loop just replace the home or 404 by your page name where you want to display your footer:
<?php
if ( is_home() ) :
get_footer( 'home' );
elseif ( is_404() ) :
get_footer( '404' );
else :
get_footer();
endif;
?>
and since you are just changing your home page footer u can just do this
<?php
if ( is_home() ) :
get_footer( 'yourName' );
else :
get_footer();
endif;
?>
Make sure of the file name to be footer-yourName.php
and then call it as follow:
get_footer( 'yourName' );
hope that helps :)
I have a div of services (full width) which I only want to display on the home page of my Wordpress site. I have used the conditional tag if(is_home()) and it is working fine. But when I added a new template this division is showing up in the template page, too. I tried using:
(if(is_home())&&(!is_page_template('blog.php'))
...but unfortunately it is not working. I have also tried it using ID and slug, and yet still this particular div is coming in the template.
I some how want this div to show up only on the homepage. You can see the services div here. The same is being displayed here, but not in other inner pages.
Use is_front_page() instead.
See Here
This code
<?php if (is_home()) : ?>
// yes, home page
<?php else : ?>
//no, not home page
<?php endif; ?>
Simply use
if(is_home() && !is_page_template('blog.php'))
Everything Should be in one condition
You can do it with any of the following conditional tags
if( is_home() && !is_page_template( 'page- template-Blog-php' ) ) {
// service div here
}
Note: If you are using template in any folder you need to give folder path too.
For example: If you are placing all the templates in 'templates' folder your code will be:
if( is_home() && !is_page_template( 'templates/page-template-Blog-php' ) ) {
// service div here
}
or you can try following code:
if( is_home() || is_front_page() ) {
// service div here
}
Braces wrongly placed:
if( (is_home()) && (!is_page_template('blog.php')) )
I am trying to redirect my WordPress homepage to the newest article automatically.
At the moment I use a redirect suggested by Spencer Cameron
function redirect_homepage() {
if( ! is_home() && ! is_front_page() )
return;
wp_redirect( 'http://homepage.com/article1', 301 );
exit;
}
add_action( 'template_redirect', 'redirect_homepage' );
Now if I post article 2 I want the homepage to automatically connect to article 2 without me adjusting the functions.php.
I want no user to see the www.example.com but only the article, so there is always a redirect to the newest article when visiting the page.
However:
I want to have the possibility to still access www.example.com/article1 (by manually typing the url) even if there is already www.example.com/article2.
How could I achieve that goal?
The answer is in Get the ID of the latest post: do a simple query to get one post (ordered by latest by default), then grab its permalink and redirect.
Don't put this type of code in functions.php, create your own mini-plugins to do it. If you want to disable this, it's just a matter of disabling a plugin, and not of editing a file.
<?php
/* Plugin Name: Redirect Homepage */
add_action( 'template_redirect', 'redirect_homepage' );
function redirect_homepage()
{
if( ! is_home() && ! is_front_page() )
return;
// Adjust to the desired post type
$latest = get_posts( "post_type=post&numberposts=1" );
$permalink = get_permalink( $latest[0]->ID );
wp_redirect( $permalink, 301 );
exit;
}
A solution from a friend:
Replace index.php in the template folder with the following:
<?php global $query_string; query_posts($query_string.'&posts_per_page=1'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php header('Location: '.get_permalink()); ?>
<?php endwhile; ?>
Thanks for helping me out