user1968657 user1968657 - 2 months ago 7
PHP Question

Wordpress: Conditional Statements same for blog and frontpage?

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,

Answer

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.