Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?"

share|improve this question
    
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 Jun 28 '11 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. –  Chip Bennett Jun 28 '11 at 16:48
add comment

2 Answers

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.

share|improve this answer
add comment

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' ) )
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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