0

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

    <!-- 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(); ?>

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

输出代码:

<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问题并简化此代码。

4

1 回答 1

0

根据Codex Query Posts Page ,法典建议不要将 query_posts 用于二级循环

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

于 2013-01-22T03:20:09.797 回答