What are the different ways to detect home page in wordpress?
Solution 1
is_front_page()
is what you want.
I assume, by the fact that is_home()
is not working, that your home page is static, according to the settings in wp-admin.
is_home()
returns true on your main blog page whereas is_front_page()
returns true on which ever page is defined as your front page, feed or not.
From codex:
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.
Solution 2
I just do the following:
if ( $_SERVER["REQUEST_URI"] == '/' ) { }
It works and doesn't overcomplicate things, especially as is_front_page()
and is_home()
don't always work as you'd expect them to.
Solution 3
With Twenty Ten I use:
<?php
if ( $_SERVER["REQUEST_URI"] == '/' ) { ?>
<h1 class="site-title"><?php bloginfo( 'name' ); ?></h1>
<h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
<?php
} else { ?>
<p class="site-title"><?php bloginfo( 'name' ); ?></p>
<p class="site-description"><?php bloginfo( 'description' ); ?></p>
<?php } ?>
Works like a charm... $_SERVER is the one I always use and it always works.
Solution 4
from outside the loop:
if(get_option("page_on_front") == $post->ID){
//do front page stuff here
}
Tushar Ahirrao
I know Javascript, HTML5, CSS3, Java, PHP and more.... I enjoy building things, learning programming languages, listening to music.
Updated on July 27, 2022Comments
-
Tushar Ahirrao almost 2 years
What are the different ways to detect wordpress homepage
except
is_front_page()
andis_home()
Thanks
-
Ikram Hawramani about 10 yearsThis seems to be the only check that works reliably if you have a category as the front page.
-
Eric K almost 4 yearsI was also able to use this approach in the WP admin since
is_front_page()
won't work there. -
LaZza over 2 yearsthis method worked great for me - is_front_page() and is_home() had no effect as I didnt have a static page set as the home page, just a list of recent posts
-
axew3 about 2 yearswhat about with this solution, if WP is installed into a subfolder? localhost/wp