• Resolved edwinbradford

    (@edwinbradford)


    I’m changing between black and white images on different pages by using is_page_template(pagename.php) to set a string value to either _rvs (stands for reverse) or empty and then inserting the returned string into the image URL.

    Everything was working across several pages. If I add a post loop to my home page ‘front-page.php’ the correct string is passed to images above the loop but not to images beneath the loop. I read the codex relating to <?php wp_reset_query(); ?> and integrated it after the loop but it does not fix the problem.

    This is in my functions.php…

    // Set page styles
    function reverse_styles() {
    	$reverse = '_rvs';
    	if ( !is_page_template('about.php') && !is_page_template('front-page.php') ) {
    		$reverse = '_rvs';
    	} else {
    		$reverse = '';
    	}
    	return $reverse;
    }

    I call the function in my page as follows…
    <img class="myClass" src="<?php echo get_stylesheet_directory_uri() ?>/images/imageName<?php echo reverse_styles() ?>.png" alt="" />

    The resulting URL in the browser is either .../images/imageName.png or .../images/imageName_rvs.png

    Any suggestions why it breaks after the loop?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try changing !is_page_template('front-page.php') to !is_front_page(). I’m pretty sure that is_page_template only works with custom templates.

    Thread Starter edwinbradford

    (@edwinbradford)

    Thanks, esmi that was quick. I printed<?php echo reverse_styles() ?>directly within the page above and beneath the loop and the returned string changes in the browser. No luck with !is_front_page() I’m afraid, if it makes any difference my Reading Settings are set to a static Front page referencing the template name of front-page.php which is “Home MySite”.

    Its very late here, I’ll have to look at it tomorrow. Thanks for the suggestion.

    Thread Starter edwinbradford

    (@edwinbradford)

    Jeez, that was ridiculously difficult to solve and not documented anywhere in the codex. The solution, which I found by luck alone, is here.

    It *really* should be in the docs and that thread is two years old. Creating a new Query object instead of using $wp_query solved it. Calling is_front_page() is_page(pageName) or is_page_template('filename.php') will all fail if you use $wp_query as normal even if you reset it after the loop.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘is_page_template function breaks after the loop’ is closed to new replies.