0

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

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; ?>

4

2 回答 2

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; ?>
于 2013-03-20T04:10:01.750 回答
0

根据法典:

由于某些全局变量在循环期间被覆盖,is_page() 将不起作用。为了在循环之后使用它,您必须在循环之后调用 wp_reset_query()。

在我看来,您在循环中使用它。

于 2013-03-24T05:12:30.780 回答