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

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.

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: 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,
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
}

Make something appear only on first page in Wordpress

How can I make a banner only appear on the index page of my wordpress theme but when the click to the second or any other page from the pagination of recent posts I do not want this to appear. Any suggestions?
Thanks
You can use the conditional is_paged()
if( !is_paged() ) {
// Show what you wanna show
}
It's only going to show on the first page, on page 2, 3, 4... it's not going to show.
You can use below condition...
if(is_home() || is_front_page()) {
// Write your code which you want to display on home page only
}

External Page Displaying Properties of [If (Home Page)]

I have a slider that displays only on our homepage with the following condition
<?php if( is_home() ) : ?>
It works properly with one exception, external pages located outside of the sites main folder that call the header display the slider as well.
Is there a way to designate a page as "not home/front"?
<?php is_front_page(); ?>
change is_home() to that and it should fix it right up.

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