Support » Themes and Templates » [Theme: Thematic] wp_reset_query() has failed me. Now what?

  • Resolved oacassidy

    (@oacassidy)


    I’m making a child theme and I’m trying to do something I thought would be simple: have a slideshow show up on the home page only. Somehow it’s not working. I’ve been searching high and low, but everyone else’s problem is solved by wp_reset_query();

    Here’s the code, it’s in my child theme’s functions.php:

    function add_slider() {
    	echo do_shortcode('[metaslider id=197]');
    }
    wp_reset_query();
    if (is_home() || is_front_page()) {
    	add_action('thematic_belowheader','add_slider','1');
    } else {
    	//YOU GET NOTHING! YOU LOSE! GOOD DAY SIR.
    }

    (Yes, all this is between php tags.) This is driving me nuts. I’ve tried a bunch of different variations, including using is_page. But the conditional just won’t work. Minus if and else (ie, just the function and add_action) it works fine, but I don’t want the slider to show up on every page.

    I’m a beginner with WP and don’t have much experience with PHP and am deeply confused by this whole “loops! queries! looped queries!” business that people start talking about when I desperately google things like “why wp_reset_query no work? how do I php? why is there this bad?” I just don’t get what’s happening or why this isn’t working. I add a function to echo is_home() below the header and it returns true on the home page, false on the other pages — so if it knows that the home page is the home page, why can’t it execute the conditional?!

    I’m going to go work on some nice, gentle CSS stuff before I tear my hair out. Thank you.

Viewing 1 replies (of 1 total)
  • Solved it myself! Actually almost started crying with relief. This is for work and I’m at work so that was weird.

    On the off chance anyone ever finds this thread, here’s what was wrong and how I fixed it:

    In Conditional Tags, you’ll notice this warning:

    Warning: You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). For themes, this means <b>the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function.</b>

    Which was further clarified by the people in this thread, where it is pointed out that because functions.php loads first, is_home() and is_front_page() will return false if run at the time functions.php is loaded because the page doesn’t know if it’s the home page yet or not.

    So basically I needed an extra layer so that the function and conditional add_action above don’t get loaded until after the page knows it’s the home page. This is the final product:

    function add_slider() {
    	echo do_shortcode('[metaslider id=197]');
    }
    function really_add_slider() {
    	if (is_home() || is_front_page()) {
    		add_action('thematic_belowheader', 'add_slider','1');
    	} else {
    		//YOU GET NOTHING. YOU LOSE. GOOD DAY SIR.
    	}
    }
    add_action('posts_selection','really_add_slider');

    MIRACULOUS. There’s probably a better, more efficient way to do this, but this is what I came up with and it WORKS YOU GUYS IT TOTALLY WORKS. No wp_reset_query needed.

Viewing 1 replies (of 1 total)
  • The topic ‘[Theme: Thematic] wp_reset_query() has failed me. Now what?’ is closed to new replies.