Wordpress: Conditional Statements same for blog and frontpage? - php

I have added a video to the homepage header of my wordpress theme but it also displays on the blog page.
my php is
if ( is_home() || is_front_page() ) {
echo '
<div class="overlay-caption">
<h1 class="home-h1">Join Us</h1>
With over 30 years of experience your adventure with us will be fun, memorable and help you pursue your passion for photography.
</div>
<div class="videoWrapper">
<iframe width="560" height="315" src="https://www.youtube.com/embed/3tmFvHviYcY?rel=0&autoplay=1&modestbranding=1&controls=0&loop=1&playlist=3tmFvHviYcY" frameborder="0" allowfullscreen autoplay="1"></iframe>
</div>';
}
else {}
?>
Now if i try only is_front_page it doesn’t appear on the homepage, if I try is_home it work but also shows up on the blog page.
I want the header on the blog page to be a standard header image, and I just want the homepage to display the video from the code I have included. Do I need to switch the conditional statement to a different order, or do I have the syntax completely wrong?
Thanks,

The basic difference between the is_home() and is_front_page() is as follows.
is_home()
The blog homepage is the page that shows the time-based blog content of the site.
is_home() is dependent on the site’s “Front page displays” Reading Settings ‘show_on_front’ and ‘page_for_posts’.
If a static page is set for the front page of the site, this function will return true only on the page you set as the “Posts page”.
is_front_page()
This Conditional Tag checks if the main page is a posts or a Page. This is a boolean function, meaning it returns either TRUE or FALSE. It returns TRUE when the main blog page is being displayed and the Settings->Reading->Front page displays is set to "Your latest posts", or when is set to "A static page" and the "Front Page" value is the current Page being displayed.
Hence now the code is as follows.
<?php
if(is_front_page() || is_home())
{
// The Code will execute if it is a 'Front Page'
// OR
// if the page is a 'Home Page'
}
if(is_front_page() && is_home())
{
// Here the code will execute if the page is
// A Front Page and as well as the Home Page
}
?>
Hence it depends on the situation of how you need to display the video over the page and to toggle around the condition that works on.
Or you try is_page() condition and that will make up the trick for you.
// When any single Page is being displayed.
is_page();
// When Page 42 (ID) is being displayed.
is_page( 42 );
// When the Page with a post_title of "Contact" is being displayed.
is_page( 'Contact' );
// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page( 'about-me' );
Hope this explanation would be clear for you to understand better.
Thanks:) Happy coding.

The order of the statements here is critical, try flipping them around such as:
if(is_front_page() || is_home())
You can also use:
if(!is_front_page() && is_home())

Try something like this
if ( is_front_page() && is_home() ) {
// Default homepage
}

Related

How i know if i'm on home page? wordpress or php

I want to verify if i'm on index.php page or home page in wordpress. I try with this code but doesn't work.
$homepage = "http://jocuri-copii.com/";
$currentpage = $_SERVER['REQUEST_URI'];
if($homepage==$currentpage)
{
$this->state = TRUE;
}
else
{
$this->state = FALSE;
}
I want to know if i'm on http://jocuri-copii.com/ and put state = true and else state = false. This method doesn't work.
You have two types of homepages:
The Posts Archive
A Page as homepage
To make sure that code runs as expected, independently of it being the posts archive or a page, you should do this on your theme's functions.php:
add_action ('wp', function () {
if (is_home() || is_front_page()) {
// Homepage
}
else {
// Not homepage
}
});
From is_home() reference page:
If a static page is set for the front page of the site, this function will return true only on the page you set as the “Posts page”.
From is_front_page() reference page:
If you set a static page for the front page of your site, this function will return true when viewing that page.
Try is_home() as referenced here
if (is_home()) {
# Do stuff
} else {
# Do other stuff
}
In WordPress, there are conditionals to verify the type of page requested by the viewer. For the home page, it depends upon how you have the Readings Settings (as found in Settings > Reading) configured.
is_home() is the "Posts Page"
is_front_page() is either
a static web page that you set as the primary home/front page
or the Posts Page
I know that's confusing. Let's walk through it. Go to the Settings > Reading admin page in the back-end of your website.
TIP
Here's a tip for you:
The Posts Page (the page that displays your latest posts, which is sometimes called "Blog") will **always* be true for is_home(). It is the "Home" Page. But it is not always the front page, as that is dependent upon how you setup "Reading Settings."
Default Setting
When you spin up a new WordPress website, the default setting is:
Front page display: set to "Your latest posts"
In this configuration, both is_home() and is_front_page() will be set to TRUE.
Setting Up a Static "Front Page"
If you define a static front page, then the conditionals are different.
The "front page" which is identified in the above example as "My Front Page" will do the following:
is_home() is FALSE
is_front_page() is TRUE
Notice that is_home() is not set. Why? Because is_home() really means "Posts Page." It does not mean "home page."
The Posts Page, which is identified in the above example as "Blog," will have the following states:
is_home() is TRUE
is_front_page() is FALSE
Front Page vs. Home Page
It's important to note the differences based upon how you have Reading Settings configured. If you want to see a video tutorial, I have one available for you here.
Remember, the is_home() function will always be true for the Posts Page. But it does not necessarily mean you are on the first, default page of your website.
Templates
WordPress has templates available for you to separate home from front page. You can use home.php and front-page.php.
However, from your index.php, you want to use the appropriate is_home() to note if you are the Posts Page or is_front_page() if you've defined a static front page.

Wordpress - is_home() and is_front_page() do not work

I have set everything, including set the front page to "Home" page in reading setting, here is the screenshot:
And I added:
if( !is_home() && !is_front_page()) {
echo "string";
}
But I still get "string" at the top of home page, as you can see in the screenshot.
Could anyone tells me why?
Thanks for any helps :-)
is_front_page() returns true if the user is on the page or page of posts that is set to the front page on Settings->Reading->Front page displays
So if you set about us as the front page then this conditional will only be true if showing the about us page.
is_home() return true when on the posts list page, This is usually the page that shows the latest 10 posts.
If the settings under Front page displays are left at default then the home page will return true for both is_front_page() and is_home()
An example of using is_home():
You have set your posts page to a page called News.
A user navigates there and in the header you want to show additional
navigation
You could use is_home() to do this.
Example:
// Add below code in your functions.php
add_action('loop_start', 'Test_hook_check');
function Test_hook_check(){
if( !is_front_page() ) {
echo "string";
}
}
You need to use is_front_page() for static pages.

Wordpress is_home() not working

I want to make background color on homepage is transparent and on otherpage background is blue.
I have using is_home() function but can't work. This is my code
<?php
if(is_home()){
// we are on the home page
echo '<header id="masthead" class="site-hulu" role="banner">';
}else {
echo '<header id="masthead" class="site-header" role="banner">';
}
?>
How to fix its guys? Thanks before for your helping
Might I suggest using is_front_page() instead
See here https://codex.wordpress.org/Function_Reference/is_home specifically
Blog Posts Index vs. Site Front Page
Since WordPress 2.1, when the static front page functionality was introduced, the blog posts index and site front page have been treated as two different query contexts, with is_home() applying to the blog posts index, and is_front_page() applying to the site front page.

WordPress conditional statement for front-page or inner page

I need help with the templating in wordpress.
On the front-page I want a custom static header, while all the other pages should use the title for their page instead.
Also, if the page is not the front-page I want to add some custom HTML next to the title.
What is the best way to do this?
You can do it like this, including the following in header.php (I've just added some example content);
<?php
if ( is_front_page() ) {
echo '<div id="homestuff">Home stuff in here</div>';
} elseif (is_page()) {
echo '<div id="pagestuff">'. the_title() .'</div>';
}
?>
Take note, there is also a is_home() in Wordpress and it's sometimes easy to get in a bit of a tangle about whether is_front_page() or is_home() is the correct one to use, this stackexchange answer is a good source of info about that;
Whether to use is_front_page() or is_home() in Wordpress

How can I tell my wordpress to do something in the header if its a blog page?

In a previous post (Don't show this if the url contains the following) I asked how I would go about having my header echo a div if the user loaded a URL with /blog in the header.
What I didn't take into consideration, was that I don't want the div to display if its not just got /blog in the url, but if its any blog post, not just the index page of the blogs.
How do I run a bit of code from my header.php if the page I'm looking at is a blog post?
You would use the Wordpress functions is_single() and is_page.
if(!is_single() && !is_page() && !is_home() && !is_archive())
{
/* This will not display for any post, page, the home page, or an archive.
You can remove each is statement according to your needs */
}
For only posts, only use is_single, the same for page, home, and archive.
The full listing of is_statements can be found here. Here are some others:
is_home() : Home Page
is_front_page : Front Page
is_single() : Single Post
is_admin() : Admin Panel
is_sticky() : Sticky Post
is_page() : Page
is_category
is_tag
is_author
It's been long since I've played with wordpress but you can achieve a lot with using the conditional tags:
http://codex.wordpress.org/Conditional_Tags
Seems like is_single() might do what you want. Eg:
if (!is_single())
{
// display div
}

Categories