首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简化wordpress中包含posts代码的while循环

简化wordpress中包含posts代码的while循环
EN

Stack Overflow用户
提问于 2013-01-22 11:01:48
回答 1查看 484关注 0票数 0

下面的代码应该显示页面的内容,后跟某些页面内容。

代码语言:javascript
复制
    <!-- Section -->
    <section>
    <?php if (have_posts()): while (have_posts()) : the_post(); ?>

        <!-- Article -->
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

            <!-- Posts for homepage -->
            <?php
                if ( is_front_page() ) { ?>
                    <?php the_content(); ?>

                    <!-- Show page content according to page ID -->
                    <div class="title-home clearfix">
                        <div class="four title-home-text">Services Spotlight</div>
                        <div class="four title-home-text" style="margin-left: 115px;">Industry Expertise</div>
                        <div class="four title-home-text" style="margin-left: 125px;">Features &amp; Benefits</div>
                    </div>
                    <div class="four-wrapper clearfix">
                    <div class="four-container">
                        <div class="four-col line"> 
                         <?php
                            query_posts('page_id=40');
                            while (have_posts()): the_post();
                               the_content();
                            endwhile;
                        ?>
                        </div>
                        <a href="" class="read-morebtn"> read more </a>
                    </div>

                    <div class="four-container">    
                        <div class="four-col line"> 
                         <?php
                            query_posts('page_id=41');
                            while (have_posts()): the_post();
                               the_content();
                            endwhile;
                        ?>
                        </div>
                        <a href="" class="read-morebtn"> read more </a>
                    </div>

                    <div class="four-container">    
                    <div class="four-col line"> 
                     <?php
                        query_posts('page_id=42');
                        while (have_posts()): the_post();
                           the_content();
                        endwhile;
                    ?>
                    </div>
                    <a href="" class="read-morebtn"> read more </a>
                    </div>


                    <div class="four-container">    
                        <div class="four-col line"> 
                            <?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('widget-area-3')) ?>
                        </div>
                    </div>

                    </div>


                <?php
                } 
                else {
                ?>
                    <h1><?php the_title(); ?> </h1>
                    <?php  the_content(); ?>
                <?php } ?>
            <!-- end post homepage -->




            <br class="clear">

            <?php edit_post_link(); ?>

        </article>
        <!-- /Article -->

        <?php endwhile; ?>

        <?php else: ?>

        <!-- Article -->
        <article>
            <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
        </article>
        <!-- /Article -->

    <?php endif; ?>

    </section>

    <!-- /Section -->

<?php get_footer(); ?>

但是,它将最后一个查询显示为循环。

输出代码:

代码语言:javascript
复制
<article id="post-6" class="post-6 page type-page status-publish hentry">
<article id="post-42" class="post-42 page type-page status-publish hentry">

post-42不应显示在文章中。

另外,我知道使用while循环的代码并不简单。我想修复article问题并简化这段代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-22 11:20:10

根据Codex Query Posts Page,食典委建议不要对二级循环使用query_posts

看起来你是在践踏主循环(文章),因为query_posts改变了主循环。如果您想使用query_posts,建议您在完成后调用wp_reset_query()。首选方法是使用WP_query()。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14450882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档