WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Why is is_home not working? (15 posts)

  1. jbickers
    Member
    Posted 6 years ago #

    Someone please help, as I'm pulling out what little hair I have left ... cannot get is_home to behave properly.

    Nutshell: We need to show different banner ads on the sidebar of the front page than on any other site pages. So I'm using conditional tags and includes to do this:

    <?php
    
    if 	(is_home()) {
    	include('tiles_front.inc.php');
    } else {
    	include('tiles_default.inc.php');
    }	
    
    ?>

    ... but even when I'm on the home page, that returns the code from the second include.

    It is a static home page, and I have a feeling this is the crux of the issue. Tried plugins, tried using is_page any number of different ways (page slug, page ID, page title), but it never recognizes the home page as the home page.

    Help?

  2. moshu
    Member
    Posted 6 years ago #

    And what did you find when reading the documentation about the conditional tags?
    Conditional_Tags#The_Front_Page

  3. jbickers
    Member
    Posted 6 years ago #

    And what did you find when reading the documentation about the conditional tags?

    I find that the exact tag I need doesn't exist until version 2.5, and our site uses 2.2.2 for a number of very specific reasons.

    Any other way to positively identify the default page?

  4. wpcast
    Member
    Posted 6 years ago #

    Yep, is_home() doesnt seem to work when home is set to a static page. I tested this on 2.2

  5. wpcast
    Member
    Posted 6 years ago #

    Tried a few other things which didn't work either. I think you might want to consider parsing out the URL and displaying your ads based on what the url looks like. For example using something like:

    $url_vars = explode("/",$_SERVER['REQUEST_URI']);

    and then testing the parts of the url that make sense for your domain using

    if($url_vars[1] == "something"){
    }
    else{
    }

    the number in $url_vars[#] refers to roughly to parts of the url seperated by a / depending on your url this may by something other then 1.

  6. jbickers
    Member
    Posted 6 years ago #

    I feel like that's getting close, but now it's recognizing every page as the front page. Our WP install exists within a subdirectory called CMS, so the home page is http://www.mydomain.com/CMS. When I test for if $url_vars[2} == CMS, it returns true no matter what page I'm on.

    I've tried this:

    <?php
    
    $url_vars = explode("/",$_SERVER['REQUEST_URI']);
    
    if($url_vars[2] == "/CMS" || $url_vars[3] == ""){
    	include('tiles_front.inc.php');
    } else {
    	include('tiles_default.inc.php');
    }	
    
    ?>

    but that fails too, returning the tiles_front include for every page and post on the site.

    ??

  7. Ivovic
    Member
    Posted 6 years ago #

    of course it does... you're using the logical OR (||) instead of AND (&&)

    you want both those conditions to be true, so you want AND not OR.

  8. jbickers
    Member
    Posted 6 years ago #

    Doh, yes, right ... but even with &&, it's returning tiles_front on every page, including the front page. So something else is wrong in there, too.

  9. wpcast
    Member
    Posted 6 years ago #

    This is working for my test install of 2.2

    if(!$url_vars[2]){
        echo "home";
    } else {
        echo "not home";
    }

    sounds like yours needs to be !$url_vars[3]

  10. jbickers
    Member
    Posted 6 years ago #

    wpcast, that's now showing the homepage include on every page, not just the home page ...

    GRR! Thanks so much for helping with this ... I'm really grateful.

  11. wpcast
    Member
    Posted 6 years ago #

    i would echo out some test text with the different vars to see which one comes up empty on home and populated on other pages.

    <?php
    $num = 0;
    foreach ($url_vars as $var){
     echo "$num : $var <br />";
     $num++;
    }
    ?>

    Put this is the sidebar and test out your navigation watching the output to see which number is empty on the homepage but populated on non homepages. Thats the number you need to put in $url_vars[ here ]

  12. iridiax
    Member
    Posted 6 years ago #

    Since I didn't want to use a static page as my front page for one WordPress site, I instead used a modified version of index.php for the front page and created template files for every other type of web page on my blog so index.php is only used for the front page. You may wish to consider this as an option if you can't get your current setup to work.

    http://codex.wordpress.org/Templates_Hierarchy

  13. jbickers
    Member
    Posted 6 years ago #

    wpcast, no number is empty on the homepage but populated on non-homepages. The homepage returns this:

    0 :
    1 : index.php

    Non-homepage return this:

    0 :
    1 : CMS
    2 : index.php?p=27309

    Any ideas?

  14. jbickers
    Member
    Posted 6 years ago #

    Nevermind, I figured out a solution - just a brute force comparison to the URL:

    <?php
    
    $url_test = $_SERVER['REQUEST_URI'];
    
    if($url_test == "/index.php" || $url_test == "/CMS/index.php"){
    	include('tiles_front.inc.php');
    } else {
    	include('tiles_default.inc.php');
    }	
    
    ?>

    While testing, just echo the value of $url_test in your sidebar, then you can make an endless series of elseifs to create separate sidebars for any specific page or post, including the front page.

  15. wpcast
    Member
    Posted 6 years ago #

    So glad you got this working jbickers!

Topic Closed

This topic has been closed to new replies.

About this Topic