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

コーデックスは、コーデックスのクエリ投稿ページに従って、セカンダリループにquery_postsを使用しないようにアドバイスしています

query_postsがメインループを変更するため、メインループ(記事)を踏みつけているようです。query_postsを使用する場合は、完了したらwp_reset_query()を呼び出すことをお勧めします。推奨される方法は、WP_query()を使用することです。

于 2013-01-22T03:20:09.797 に答える