Wordpress条件语句不起作用

时间:2013-03-20 03:51:33

标签: wordpress

我正在设置一些条件语句来更改不同页面的页面布局。我的第一个条件是

if (is_page('15030')) {
}

该内容中的内容适用于主页,并且完美运行。然后我的下一个说

elseif (is_page('Sales')){
}

它根本不起作用。我尝试过使用页面名称,页面ID,将elseif更改为if,使用in_category - 根本没有显示任何内容。知道我应该寻找什么可能会导致这种情况吗?我注意到我将“销售”设置为一类帖子,当我创建销售页面时,它会自动显示销售类别中的帖子,这是我想要的,但我不确定是否会完全影响它?

代码位于custom_functions.php中。这是完整的代码:

function custom_template() {
if (is_page('15030')) : ?>
    <!--Technology Section-->
    <div id="content">
        <div id="post-<?php the_ID(); ?>" class="post_box top">
            <a class="scroll" id="technology"></a>
            <div class="left">
                <a href="index.php?page_id=15174"><img src="../../../images/businessbee/blog/tech-bubble.gif" alt="Technology" class="bubble"/></a>
                <h3><a href="index.php?page_id=15174">Technology</a></h3>
                <p>Technology upgrades don't have to break the bank. From improving your hardware to streamlining your IT management, we've compiled cost-effective solutions to cover your needs. </p>
                <hr/>
                <ul class="catCount">
                    <?php
                        $catList = wp_list_categories('title_li=&show_count=1&echo=0&child_of=3527&hide_empty=0');
                        $catList = preg_replace('@\<li([^>]*)>\<a([^>]*)>(.*?)\<\/a>@i', '<li$1><a$2><span class="name">$3</span></a>', $catList);
                        $catList = ereg_replace('</a> \(([0-9]+)\)', ' <span class="count">\\1</span><br class="clear"/></a>', $catList);
                        echo $catList;
                    ?>
                    <br class="clear"/>
                </ul>
                <hr/>
                <a href="index.php?page_id=15174" class="allLink">View all Technology Resources</a>
            </div><!--left-->
            <div class="right">
                <h4>Featured Resources</h4>
                    <?php global $post; ?>
                    <?php if (have_posts()) : ?>
                        <?php query_posts('cat=3527'); ?>
                       <?php while (have_posts()) : the_post(); ?>
                            <?php $categories = get_the_category(); ?>
                            <?php if ( has_post_thumbnail() ) { ?>
                                <a href="<?= the_permalink() ?>" class="box">
                                  <div class="thumbnailBox">
                                    <h3><?= the_title()?>
                                    <span>posted in <?= $categories[0]->cat_name; ?></span>
                                    </h3>
                                    <?php the_post_thumbnail('medium'); ?>
                                    <div class="overlay"></div>
                                    </div>
                                </a>
                            <?php } ?>
                    <?php endwhile; ?>
                    <?php endif; ?>
                    <?php wp_reset_query(); ?>
            </div><!--right-->
            <br class="clear"/>
            <img src="../../../images/businessbee/blog/page-div.png" alt="" class="blog-page-div"/>

            <!--SALES SECTION-->
                <a class="scroll" id="sales"></a>
                <div class="left">
                    <a href="index.php?page_id=15206"><img src="../../../images/businessbee/blog/sales-bubble.gif" alt="Sales" class="bubble"/></a>
                    <h3><a href="index.php?page_id=15206">Sales</a></h3>
                    <p>Every sales team needs a solid method for tracking leads and identifying its top performers. Take a look at these great resources, designed to help maximize your conversion rate.</p>
                    <hr/>
                    <ul class="catCount">
                    <?php
                        $catList = wp_list_categories('title_li=&show_count=1&echo=0&child_of=3536&hide_empty=0');
                        $catList = preg_replace('@\<li([^>]*)>\<a([^>]*)>(.*?)\<\/a>@i', '<li$1><a$2><span class="name">$3</span></a>', $catList);
                        $catList = ereg_replace('</a> \(([0-9]+)\)', ' <span class="count">\\1</span><br class="clear"/></a>', $catList);
                        echo $catList;
                    ?>
                        <br class="clear"/>
                    </ul>
                    <hr/>
                    <a href="index.php?page_id=15206" class="allLink">View all Sales Resources</a>
                </div>
                <div class="right">
                <h4>Featured Resources</h4>
                    <?php global $post; ?>
                    <?php if (have_posts()) : ?>
                        <?php query_posts('cat=3536'); ?>
                       <?php while (have_posts()) : the_post(); ?>
                            <?php $categories = get_the_category(); ?>
                            <?php if ( has_post_thumbnail() ) { ?>
                                <a href="<?= the_permalink() ?>" class="box">
                                  <div class="thumbnailBox">
                                    <h3><?= the_title()?>
                                    <span>posted in <?= $categories[0]->cat_name; ?></span>
                                    </h3>
                                    <?php the_post_thumbnail('medium'); ?>
                                    <div class="overlay"></div>
                                    </div>
                                </a>
                            <?php } ?>
                    <?php endwhile; ?>
                    <?php endif; ?>
                    <?php wp_reset_query(); ?>
            </div><!--right-->
            <br class="clear"/>
            <img src="../../../images/businessbee/blog/page-div.png" alt="" class="blog-page-div"/>

    </div><!-- post id div-->
    </div><!--content-->

<!--End of Homepage Content-->

     elseif (is_page('Sales')){
        <h2>Test Content</h2>
    }

<?php endif; ?>

2 个答案:

答案 0 :(得分:0)

看起来它需要if而不是elseif并包含在php标签中,如此

<?php endif; ?> // this closes the 1st if statement aka the if for is_page('15030').

<?php if ( is_page('Sales') ): ?>
<h2>Test Content</h2>
<?php endif; ?>

答案 1 :(得分:0)

根据食典委:

  

由于某些全局变量在循环期间被覆盖,因此is_page()将无效。为了在The Loop之后使用它,你必须在The Loop之后调用wp_reset_query()。

我认为你 在循环中使用它。

相关问题