为什么自定义模板没有显示为带有is_page()的页面?

时间:2012-10-17 作者:jansensan

我为一个项目创建了几个模板,实际上它是Contrast theme. 在Wordpress CMS中,我可以选择我创建的模板:

http://i.imgur.com/PYKqK.png (不能发布图片,网站说我是个新手……)

在主题的一个函数中(请参见下面的background.php),有以下条件检查:

<?php if (is_page() || is_single()) { ?>
它适用于除两个模板外的所有自定义模板。我知道这两个失败的地方实际上是页面,但是is_page() 仍然是空的,因此false.

我应该寻找什么来确保这两个模板作为页面?还是我应该找别的东西?

下面是两个模板的代码。请记住,我只涉及与内容相关的代码,而不是模板开头的if/else。

页面底部包含一个文件,用于检查is_page(). 下面也是该文件的代码。

文本内容模板(函数,返回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(); ?>
新闻模板(不起作用,不返回值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(); ?>
如您所见,背景。php包含在这些模板的底部。这个文件来自父主题,我没有编辑它。其代码如下:

<!-- 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 -->

1 个回复
最合适的回答,由SO网友:Milo 整理而成

当你打电话的时候query_posts($query); 在第二个模板中,覆盖WordPress用于返回is_ 条件。基本上,你不应该使用query_posts 在模板中。对于次循环,请使用的新实例WP_Query 相反

结束

相关推荐

Pass data between pages

你将如何解决这个问题?在一个贴子页面(single.php)上,我需要呼应出导致它的分类法的名称。因此,如果我浏览某个分类法中的帖子列表并单击某个帖子,我需要在该帖子页面(single.php)上显示之前查看过的分类法名称,这将导致出现帖子页面。分类相册。php分类法歌曲。php仅有一个的php有没有办法不使用GET、POST或COOKIE方法?