0

I have the index.php in themes directory as my homepage. How can I confirm programmatically that the homepage was called in my functions.php? is_home() and is_front_page() is true for other pages and PHP_SELF always shows index.php.

[More info]

What I'm trying to do is show some Google Web Optimizer Control scripts only on the home page (http://example.com/). The B in the A/B test is index-3 (http://example.com/index-3). But what happens is that if I hit our blog pages (http://example.com/blogs) is_home() and is_front_page() is triggering true causing the script to show in the head. I'm not sure what is causing the blogs page to trigger. I'm using the wp_print_scripts event to trigger my function in the functions.php file.

Settings>>Reading is set to "Your latest posts" the home page name is index.php located in the themes directory.

So the final question is "Is there a method to know that the function called is for the home page (http://example.com/) and not another?"

2
  • Sorry for wasting your time guys. All I had to do is look at the $_SERVER['REQUEST_URI'] I got myopic and forgot I could just look at the environment info.
    – Clutch
    Commented Jun 28, 2011 at 16:39
  • This appears to be more of a clarification of your question. If you need to clarify your question, you should just update your question directly. Otherwise, this becomes an answer to an old version of the question. Commented Jun 28, 2011 at 16:48

2 Answers 2

7

If you are using the default home page, which in most cases is a list of most recent posts, is_home() should work.

If you have set a static page to be your front page (in Settings >> Reading), is_front_page() should work. If you are not getting the results you expect using this:

if (is_front_page()) {
  //the code you want to execute
}

perhaps you can describe your situation in more detail.

1

Up-voted @supertrue, but wanted to add some clarification:

  • is_front_page() returns true when on the site front page (what is commonly referred to as the "home" page).
  • is_front_page() returns true when on the site front page, whether the front page is set to display the Blog Posts Index or a static Page.
  • is_home() returns true when displaying the Blog Posts Index.
  • is_home() returns true when displaying the Blog Posts Index, whether on the Front Page, or on a static Page.
  • A useful option for differentiating between static Page and Blog Posts Index when on the Front Page is get_option( 'show_on_front' ), which returns either page or posts

EDIT

Settings>>Reading is set to "Your latest posts" the home page name is index.php located in the themes directory.

So, based on what I stated above:

  • When on the Front Page, then is_front_page() always returns true.
  • When displaying your Blog Posts Index ("your latest posts"), is_home() always returns true.

Your Front Page is set to display your blog posts index, thus both is_front_page() and is_home() will return true.

EDIT 2

So the final question is "Is there a method to know that the function called is for the home page (http://example.com/) and not another?"

Yes! That's what is_front_page() is for.

If you need to know you're on the front page, and that the front page is static:

if ( is_front_page() && 'page' == get_option( 'show_on_front' ) )

If you need to know you're on the front page, and that the front page is your blog posts:

if ( is_front_page() && 'posts' == get_option( 'show_on_front' ) )

Not the answer you're looking for? Browse other questions tagged or ask your own question.