What are the different ways to detect home page in wordpress?
Asked Answered
G

8

9

What are the different ways to detect wordpress homepage

except is_front_page() and is_home()

Thanks

Glutelin answered 7/2, 2011 at 11:27 Comment(4)
What is wrong with is_home() ?Wangle
its not working in my blog :(Glutelin
I don't know WordPress well, but could you do it quick and easy with $isHome = ($_SERVER['REQUEST_URI'] == '/') (or whatever your homepage is)?Wangle
What is the url of your homepage? And does it return always false?Mccowyn
F
21

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.

Fabliau answered 7/2, 2011 at 13:55 Comment(0)
D
20

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.

Dever answered 7/2, 2011 at 11:59 Comment(2)
This seems to be the only check that works reliably if you have a category as the front page.Repast
what about with this solution, if WP is installed into a subfolder? localhost/wpKinghorn
L
4

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.

Latifundium answered 9/2, 2014 at 22:15 Comment(1)
this 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 postsQuod
C
3

from outside the loop:

if(get_option("page_on_front") == $post->ID){
    //do front page stuff here
}
Clairvoyance answered 19/9, 2017 at 17:7 Comment(1)
I was also able to use this approach in the WP admin since is_front_page() won't work there.Kennel
O
0

In many situations a Wordpress site can have is_home and is_frontpage both eval as true on the REAL homepage and also on the main blog page. After building sites in Wordpress for about 4 years this still bothers me.

For example if you have a site where you have your latest posts on your homepage with maybe a slider or some other homepage-centric elements, AND have another blog page, then is_frontpage and is_home will both eval as true on BOTH pages. So Wordpress does not have a clear conditional function for the true homepage, at least the way most people think of the homepage of a website.

So I agree with Liam that if you get into a confusing situation, something like if ( $_SERVER["REQUEST_URI"] == '/' ) { }

is more reliable.

Onesided answered 18/6, 2013 at 14:49 Comment(0)
R
0

For me is_front_page() and is_home() doesn't work in ways I need to check the homepage, so instead I write this condition:

global $wp;  
$current_url = home_url(add_query_arg(array($_GET), $wp->request));
if ($current_url==get_site_url()) { 
    // code for homepage 
}
Repellent answered 27/9, 2019 at 8:41 Comment(0)
K
0

To me, all previous answers, some are clearly sometime wrong, and some not working always because it depend where (stack) you need to know if you are or not into WP homepage. I did not found any valid answer here, nor elsewhere, so i'll do this instead that will work always, except in rare situations where the query string is appended into home url for some reason. In this (very rare) case, the code should be then little improved, but generally, this will work where others will fails:

if(!empty($_SERVER['REQUEST_URI']))
{
 if(substr($_SERVER['REQUEST_URI'], -1, 1) == '/'){
  $REQUEST_URI = substr($_SERVER['REQUEST_URI'], 0, -1);
 }

 $siteUrl = get_option('siteurl');

 if(substr($siteUrl, -1, 1) == '/'){
  $siteUrl = substr($siteUrl, 0, -1);
 }

if(!empty($REQUEST_URI)){
  $r = strpos($siteUrl, $REQUEST_URI);
  if($r !== false)
  { $HOMEPAGE = true; }
 } elseif ( $_SERVER['REQUEST_URI'] == '/' OR empty($REQUEST_URI) ){ $HOMEPAGE = true; }
}

var_dump($HOMEPAGE);

Ok with any permalink.

[EDITED] Do not change the global $_SERVER['REQUEST_URI'] value

Kinghorn answered 4/3, 2022 at 17:50 Comment(0)
B
-1

is_home() is the way to go.

Have you tried this method and it doesn't work? If yes, it has usually something to do with the mod_rewrite settings or the wordpress settings itself.

Bulldog answered 7/2, 2011 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.