WordPress.org

Ready to get started?Download WordPress

Forums

Issue with is_home() condition on WP 3.3 (14 posts)

  1. eva-MyClientIsRich
    Member
    Posted 2 years ago #

    Hello !

    I'm working on a theme using different thumbnail sizes on blog homepage. My code was working fine before the update. And now... It looks like the condition if(is_home() && has_tag(array('place1', 'place2', 'place5'))) does not work anymore... When I remove the is_home() part, it works fine. But I need it... Here is the code.

    1st try, working fine before WP 3.3 :

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
    	<div class='thumbnail-image'>
    		<a href='<?php the_permalink() ?>'>
    			<?php
    			if(is_home() && has_tag(array('place1', 'place2', 'place5'))) {
    					the_post_thumbnail('home-smallpost-thumbnail');
    			}
    			else{
    				the_post_thumbnail('home-bigpost-thumbnail');
    			}
    			?>
    		</a>
    	</div>
    
    ...

    2nd try was this :

    if(is_home()) {
    	if(has_tag(array('emplacement1', 'emplacement2', 'emplacement5'))) {
    		the_post_thumbnail('home-smallpost-thumbnail');
    	}
    }

    3rd try was the same, without the if(is_home()){} part (so only the has_tag() part) and it works fine...

    I'm completely lost. Why should the is_home() condition break ? Did you experience this kind of problem lately ? Do you see any explaination ?

    I hope to hear from someone soon !

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    you may have used custom queries in you main section, which could have distorted the original query string and the idea of 'home' -
    try to add wp_reset_query(); before your if ( is_home() ) or before your loop.

  3. markparolisi
    Member
    Posted 2 years ago #

    since WP 3.0 you should use wp_reset_postdata()

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    @markparolisi
    Didn't know that. There's nothing on the codex page that it's a deprecated function.
    http://codex.wordpress.org/Function_Reference/wp_reset_query

  5. eva-MyClientIsRich
    Member
    Posted 2 years ago #

    Hi ! Thanks for trying to help !
    Tried both solutions, and the same issue continues...

    To be sure, I tried to use a regular php function, to get the current page (homepage) url and see if it matches my $homeUrl, and it works. The is_home() function is still not working, even when I reset queries... Until I find a solution about is_home(), I'll use my function (which does the same).

    <?php
    $homeUrl = "http://mysite.com/";
    $myUrl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if($myUrl == $homeUrl && has_tag(array('place1', 'place2', 'place5'))) {
    	the_post_thumbnail('home-smallpost-thumbnail');
    }
    else{
    	the_post_thumbnail('home-bigpost-thumbnail');
    }
    ?>

    Related question : are we allowed to use several times is_home() on one page (I use it once in header.php and was trying to use it in content.php) ? I read somewhere that it may cause some troubles... (I think it should not).

  6. keesiemeijer
    moderator
    Posted 2 years ago #

    On what template file are you doing this. If this is a static home Page you maybe have to use is_front_page(): http://codex.wordpress.org/Conditional_Tags#The_Main_Page

    are we allowed to use several times is_home() on one page

    yes you are.

  7. eva-MyClientIsRich
    Member
    Posted 2 years ago #

    I'm using is_home() on the blog homepage, not on a static page. And that's the blog which is configured as the home page. I also thought about is_front_page() but double checked that my blog was the homepage. That's weird.

  8. keesiemeijer
    moderator
    Posted 2 years ago #

    So you're using it in index.php?

  9. eva-MyClientIsRich
    Member
    Posted 2 years ago #

    in content.php (it is a theme based on twenty eleven), which is included in index.php.

  10. keesiemeijer
    moderator
    Posted 2 years ago #

    Can you put this in index.php before the loop (for testing). Before <?php if ( have_posts() ) : ?> if your index.php has that:

    <?php
    global $wp_query;
    echo '<pre>';
    print_r($wp_query);
    echo '</pre>';
    ?>

    and see if you if is_home is set:
    [is_home] => 1

  11. eva-MyClientIsRich
    Member
    Posted 2 years ago #

    Yes, the result is [is_home] => 1 on index.php

    Just to try, I pasted this in content.php, and it is not set there : [is_home] =>

  12. keesiemeijer
    moderator
    Posted 2 years ago #

    Can you paste and submit the full code of index.php and content.php of your theme into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

    Also try:
    - deactivating all plugins to see if this resolves the problem? If this works, re-activate the plugins one by one until you find the problematic plugin(s).

    - switching to the default theme to rule out any theme-specific problems.

  13. eva-MyClientIsRich
    Member
    Posted 2 years ago #

    Here is the index.php full code : http://pastebin.com/q2wpQTqF

    And here is content.php : http://pastebin.com/RkhsA3jx

    I already tried with all my plugins desactivated. Looks like they do not play a role in this issue.

    With default Twenty Eleven theme, my condition works in content.php
    I'm looking at my code, trying to see anything different from Twenty Eleven default theme (as I mentioned, my own theme is based on this theme so it is very similar) which would break the is_home() condition...

    Thanks for the kind help, I'm afraid I can't paste the link to the website, it is not public yet.

  14. keesiemeijer
    moderator
    Posted 2 years ago #

    You have 5 loops on your index.php and you query the loop on all of them. If it's not the main loop (with pagination) it's better to use the $the_query = new WP_Query( $args ); method for these loops as this doesn't interfere with the main query.
    http://codex.wordpress.org/Function_Reference/WP_Query#Usage

    Or on index.php use the global query_string on each query_posts and set is_home after each query_posts:

    global $query_string;
    query_posts("$query_string&tag=emplacement5");
    $wp_query->is_home = 1;

Topic Closed

This topic has been closed to new replies.

About this Topic