Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created a couple of templates for a project, which is in fact a child theme of the Contrast theme. In the Wordpress CMS, I can select the templates I created:

http://i.imgur.com/PYKqK.png (Can't post image, I'm too much of a newbie the site says...)

In one of the functions of the theme (see background.php below), there is this check for conditions:

<?php if (is_page() || is_single()) { ?>

Which works on all my custom templates but two. I know for a fact that the two places where this fails are actually pages, but the value returned by is_page() is still empty, thus false.

What should I look for in order to make sure these two templates are taken as pages? Or is it something else that I should look for?

Below is the code for two templates. Keep in mind that I only touched the code relative to the content, not the if/elses at the beginning of the templates.

At the bottom of the page is included a file that makes that check for the is_page(). Code for that file below as well.

Textual Content Template (functional, returns a value for is_page()):

<?php
/*
Template Name: Textual Content Page
*/
?>

<?php get_header(); ?>

    <?php
        // transparent_layer
        if(meta(array('id' => $post -> ID, 'meta' => 'transparent_layer')) == '0')
        {
            if(option('transparent_layer') == '0')
            {
                $transparent = false;
            }
            elseif(option('transparent_layer') == '1')
            {
                $transparent = true;
            }
        }
        elseif(meta(array('id' => $post -> ID, 'meta' => 'transparent_layer')) == '1')
        {
            $transparent = false;
        }
        elseif(meta(array('id' => $post -> ID, 'meta' => 'transparent_layer')) == '2')
        {
            $transparent = true;
        }


        // content_position
        if(meta(array('id' => $post -> ID, 'meta' => 'content_position')) == '0')
        {
            if(option('content_position') == '0')
            {
                $content_position = '0';
            }
            elseif(option('content_position') == '1')
            {
                $content_position = '1';
            }
            elseif(option('content_position') == '2')
            {
                $content_position = '2';
            }
        }
        elseif(meta(array('id' => $post -> ID, 'meta' => 'content_position')) == '1')
        {
            $content_position = '0';
        }
        elseif(meta(array('id' => $post -> ID, 'meta' => 'content_position')) == '2')
        {
            $content_position = '1';
        }
        elseif(meta(array('id' => $post -> ID, 'meta' => 'content_position')) == '3')
        {
            $content_position = '2';
        }


        //portrait_fit
        if(meta(array('id' => $post -> ID, 'meta' => 'portrait_fit')) == '1')
        {
            $portrait = '1';
        }
    ?>

    <!-- content start -->
    <div class="content">
        <?php get_sidebar(); ?>

        <div class="sliderContent">
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

            <h1><?php the_title(); ?></h1>

            <div class="content-body">
                <?php the_content(__('Read More &raquo;', THEMENAME)); ?>

                <?php endwhile; else: ?>
                <p><?php _e('Nothing found.', THEMENAME); ?></p>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <!-- content end -->

    <?php require_once(TEMPLATEPATH . '/assets/includes/theme-background.php'); ?>

<?php get_footer(); ?>

News Template (not functional, returns no value for is_page()):

<?php
/*
Template Name: News Page
*/
?>

<?php get_header(); ?>

    <?php
        // transparent_layer
        if(meta(array('id' => $post -> ID, 'meta' => 'transparent_layer')) == '0')
        {
            if(option('transparent_layer') == '0')
            {
                $transparent = false;
            }
            elseif(option('transparent_layer') == '1')
            {
                $transparent = true;
            }
        }
        elseif(meta(array('id' => $post -> ID, 'meta' => 'transparent_layer')) == '1')
        {
            $transparent = false;
        }
        elseif(meta(array('id' => $post -> ID, 'meta' => 'transparent_layer')) == '2')
        {
            $transparent = true;
        }


        // content_position
        if(meta(array('id' => $post -> ID, 'meta' => 'content_position')) == '0')
        {
            if(option('content_position') == '0')
            {
                $content_position = '0';
            }
            elseif(option('content_position') == '1')
            {
                $content_position = '1';
            }
            elseif(option('content_position') == '2')
            {
                $content_position = '2';
            }
        }
        elseif(meta(array('id' => $post -> ID, 'meta' => 'content_position')) == '1')
        {
            $content_position = '0';
        }
        elseif(meta(array('id' => $post -> ID, 'meta' => 'content_position')) == '2')
        {
            $content_position = '1';
        }
        elseif(meta(array('id' => $post -> ID, 'meta' => 'content_position')) == '3')
        {
            $content_position = '2';
        }


        // portrait_fit
        if(meta(array('id' => $post -> ID, 'meta' => 'portrait_fit')) == '1')
        {
            $portrait = '1';
        }
    ?>


    <!-- content start -->
    <div class="content">
        <?php get_sidebar(); ?>

        <div class="sliderContent">

            <h1><?php the_title(); ?></h1>

            <div class="content-body news-items-list">
                <?php
                    // loop through posts in news category
                    // -1 means all posts... not really intuitive
                    // see http://www.binarymoon.co.uk/2010/03/5-wordpress-queryposts-tips/
                    $query = array  (
                                        'posts_per_page' => -1,
                                        'category_name' => 'news'
                                    );
                    query_posts($query);
                    while(have_posts()) : the_post();

                    // get post raw date
                    $date = get_the_date();
                ?>
                    <a 
                        href="<?php the_permalink(); ?>" 
                        class="news-link"
                        >
                        <span class="title"><?php the_title(); ?></span>
                        <span class="date"><?php echo $date; ?></span>
                    </a>

                <?php
                    endwhile;
                ?>
            </div>
        </div>
    </div>
    <!-- content end -->

    <?php require_once(TEMPLATEPATH . '/assets/includes/theme-background.php'); ?>

<?php get_footer(); ?>

As you can see, background.php is included at the bottom of these templates. This file comes from the parent theme, which I did not edit. Below is its code:

<!-- loading start -->
<div class="loading">
    <img src="<?php bloginfo('template_url'); ?>/assets/images/loading.gif" alt ="Loading" />
</div>
<!-- loading end -->


<!-- background start -->
<div id="background">

        <?php if (is_page() || is_single()) { ?>

            <?php if (meta(array('id' => $post->ID, 'meta' => 'post_background')) == '1') { ?>

                <style type="text/css">
                    #background { background: <?php echo meta(array('id' => $post->ID, 'meta' => 'post_background_color')); ?>; }
                </style>

            <?php } elseif (meta(array('id' => $post->ID, 'meta' => 'post_background')) == '2') { ?>

                <img src="<?php echo meta(array('id' => $post->ID, 'meta' => 'post_background_image')); ?>" alt="" class="background" />

            <?php } elseif (meta(array('id' => $post->ID, 'meta' => 'post_background')) == '3') { ?>

                <script type="text/javascript">
                    var flashvars = { flv : '<?php echo meta(array('id' => $post->ID, 'meta' => 'post_background_flash_video')); ?>' };
                    var params = { wmode: 'transparent' };
                    swfobject.embedSWF('<?php bloginfo('template_url'); ?>/assets/flash/background.swf', 'background', '100%', '100%', '9.0.0', 'expressInstall.swf', flashvars, params);
                </script>

            <?php } elseif (meta(array('id' => $post->ID, 'meta' => 'post_background')) == '4') { ?>

                <?php if (meta(array('id' => $post->ID, 'meta' => 'post_background_google_maps_gradient')) == '1') { ?>
                <div class="gradient">
                    <div class="left">&nbsp;</div>
                    <div class="right">&nbsp;</div>
                </div>
                <?php } ?>

                <iframe width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="<?php echo meta(array('id' => $post->ID, 'meta' => 'post_background_google_maps')); ?>"></iframe>

            <?php } elseif (meta(array('id' => $post->ID, 'meta' => 'post_background')) == '5') { ?>

                <script type="text/javascript">
                    jQuery(function($){
                        $.supersized({
                            slide_interval      : <?php echo meta(array('id' => $post->ID, 'meta' => 'slideshow_timeout')) ?>,
                            transition          : <?php echo meta(array('id' => $post->ID, 'meta' => 'slideshow_effect')) ?>,
                            transition_speed    : <?php echo meta(array('id' => $post->ID, 'meta' => 'slideshow_speed')) ?>,
                            slide_links         : 'blank',
                            slides              : [<?php if (meta(array('id' => $post->ID, 'meta' => 'slideshow_category_type')) == '1') {
                                                $slideshow_categories = '&slideshow-categories=' . meta(array('id' => $post->ID, 'meta' => 'slideshow_category')); }
                                                query_posts('post_type=slideshow' . $slideshow_categories. '&orderby=date&showposts=' . meta(array('id' => $post->ID, 'meta' => 'slideshow_show_posts')));
                                                if (have_posts()) { while (have_posts()) { the_post();
                                                $slideshow_images = $slideshow_images . '{image : \'' . image(array('image' => get_the_post_thumbnail($post->ID), 'tag' => false)) . '\'},';
                                                }} wp_reset_query(); echo substr($slideshow_images, 0, -1); ?>]
                        });
                    });
                </script>

                <div id="progress-back" class="load-item">
                    <div id="progress-bar"></div>
                </div>

            <?php } elseif (meta(array('id' => $post->ID, 'meta' => 'post_background')) == '6') { ?>

                <video id="video" class="background">
                    <?php if (meta(array('id' => $post->ID, 'meta' => 'post_background_mp4_video')) != '') { ?>
                    <source src="<?php echo meta(array('id' => $post->ID, 'meta' => 'post_background_mp4_video')); ?>" type="video/mp4" />
                    <?php } ?>
                    <?php if (meta(array('id' => $post->ID, 'meta' => 'post_background_webm_video')) != '') { ?>
                    <source src="<?php echo meta(array('id' => $post->ID, 'meta' => 'post_background_webm_video')); ?>" type="video/webm" />
                    <?php } ?>
                    <?php if (meta(array('id' => $post->ID, 'meta' => 'post_background_ogv_video')) != '') { ?>
                    <source src="<?php echo meta(array('id' => $post->ID, 'meta' => 'post_background_ogv_video')); ?>" type="video/ogg" />
                    <?php } ?>
                </video>

                <script type="text/javascript">
                    var video = document.getElementById('video');

                    if (typeof video.loop == 'boolean') {
                        video.loop = true;
                    }

                    else {
                        video.addEventListener('ended', function () {
                        this.currentTime = 0;
                        this.play();
                        }, false);
                    }

                    video.play();
                </script>

            <?php } ?>

            <?php if (meta(array('id' => $post->ID, 'meta' => 'post_background_image_pattern')) == '1') { ?>
                <div class="pattern pattern-0<?php echo meta(array('id' => $post->ID, 'meta' => 'post_background_pattern')); ?>">&nbsp;</div>
            <?php } ?>

        <?php } ?>
</div>
<!-- background end -->
share|improve this question
    
Try dumping the $wp_query variable to see what is_$ condition is met. –  Brian Fegter Oct 17 '12 at 19:44
    
Does is_singular() work? –  toscho Oct 17 '12 at 19:46
    
@BrianFegter: actually, most is_$ conditions are false (they are at the bottom of the dump I obtained) –  jansensan Oct 17 '12 at 19:52
    
@toscho: no, I get the same results with that value –  jansensan Oct 17 '12 at 19:52
    
The only is_$ condition that is true is is_category –  jansensan Oct 17 '12 at 19:54

1 Answer 1

up vote 2 down vote accepted

When you call query_posts($query); in your second template, you overwrite the global variable that WordPress uses to return the correct values for the is_ conditionals. You should basically never use query_posts in the template. For secondary loops, use a new instance of WP_Query instead.

share|improve this answer
    
A-ha! That was indeed my issue! THanks so much! –  jansensan Oct 18 '12 at 17:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.