WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Is_home php rule not working correctly

[Resolved] Is_home php rule not working correctly

  • Hello,

    I used the php condition “is_home” to restrict the loading of the Google +1 button only on my home page, not inside the internal pages.

    However, I noticed that the +1 button is also shown in the archives pages, I’m puzzled !

    For instance, if I load “http://www._my_website_.net/page/13/”, there WILL be a google +1 button, showing a zero count (unsurprisingly).
    The +1 button isn’t shown on posts or pages, it’s just shown in the archives pages. You know, when you click the “older posts” link and then navigate through the lists of older posts.

    Here’s my code :

    <?php
    if ( is_home() ) {
    echo '<br><br><g:plusone size=small></g:plusone>';
    } else {
        // do nothing, kthxbai
    }
    ?>

    Please, would you know why the is_home is ignored, and how I could fix that error ?

    Thank you very much if you can help 🙂

    (PS : I deliberately refuse to use a widget or a plugin to manage this stuff, I want it hardcoded)

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try using is_front_page() instead.

    In WordPress parlance, Home refers to the Blog Posts Index (which is why is_home() returns true on blog post archive pages). The Site Front Page is referred to as Front Page.

    So, if you want to test conditionally for your Site Front Page, then use is_front_page().

    Codex references:
    * is_home() conditional template tag
    * is_front_page() conditional template tag

    Thank you for the reply, Chip !

    I eagerly updated my code, and yet, to my surprise, the problem remains. (I did the usual double checks against unrefreshed blog cache contents and the like).

    I read, and read again the codex reference page you gave (thank you for this !), and found no mention of something I would have overlooked again, or that I would have to change…

    This is confusing.

    I can survive this gruesome situation with a bit of chance, it’s just that leaving the problem unsolved will leave a bad aftertaste…

    For instance, if I load “http://www._my_website_.net/page/13/”, there WILL be a google +1 button, showing a zero count (unsurprisingly).

    When you’re on this URL, you’re technically still on your Front Page, as far as WordPress is concerned.

    The +1 button isn’t shown on posts or pages, it’s just shown in the archives pages.

    These aren’t archive pages; you’re still on your main blog posts index.

    Try checking for the $paged global variable:

    <?php
    global $paged;
    if ( is_home() && '1' > $paged ) {
    echo '<br><br><g:plusone size=small></g:plusone>';
    } else {
        // do nothing, kthxbai
    }
    ?>

    $paged did the trick, thank you once again, Chip !

    It’s useful to learn new wordpress internals 🙂

    Just in case someone else needs it in the future, since searching “paged” returns nothing in the codex search field (go figure, I had to resort to a google site:wordpress.org search), here’s the reference :
    http://codex.wordpress.org/Category_Templates

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Is_home php rule not working correctly’ is closed to new replies.
Skip to toolbar