用於頁面的另一個循環內的帖子的Wordpress循環 - 一個頁面網站

[英]Wordpress loop for posts inside another loop for pages - one page website


I made landing page(onepage) in WordPress.

我在WordPress中制作了登陸頁面(onepage)。

I display all pages in wordpress loop.

我在wordpress循環中顯示所有頁面。

index.php:

    <?php 
    query_posts(array(
        'post_type' => 'page',
        'posts_per_page' => '-1',
        'order' => 'ASC',
        'orderby' => 'menu_order'
        ));

    $tpl_parts = array(
        '5' => 'about',
        '7' => 'team',
        '76' => 'tech',
        '81' => 'services',
        '101' => 'contact',
        );
 ?>

<?php get_header('home'); ?>


<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <?php if(array_key_exists($post->ID, $tpl_parts)) : ?>

                <?php get_template_part('template-parts/'. $tpl_parts[$post->ID], 'template'); ?>

            <?php else: ?>
                    <section id="<?php echo $post->post_name; ?>">
                    <div class="container">
                        <div class="row">
                            <?php the_content(); ?>
                        </div>
                    </div>  
                    </section>
            <?php endif; ?>

<?php endwhile; else : ?>
<?php endif; ?>

I want show posts from category services in page services.

我想要從頁面服務中的類別服務中顯示帖子。

services-template.php:

 <section id="services" class="services-section">
        <div class="container">
            <div class="row">
                <div class="col-xs-12">
                    <h2 class="text-left">Services</h2>
                </div>
            </div>
            <?php query_posts('category_name=services'); ?>
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <div class="row box-service">
                <div class="col-sm-6">
                    <?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
                </div>
                <div class="col-sm-6">
                    <h3><?php the_title(); ?></h3>
                    <p class="intro"><?php echo get_the_content(); ?></p>
                    <a href="#">More</a>
                </div>
            </div>
            <?php endwhile; else: endif; wp_reset_query(); ?>

        </div>
    </section>

Page services display all posts from services category, but under services page, wordpress loop display posts from category services again, and contact section is removed.

頁面服務顯示來自服務類別的所有帖子,但在服務頁面下,wordpress循環再次顯示來自類別服務的帖子,並刪除聯系人部分。

What is wrong in this code ? I add wp_reset_query(); but problem still exist.

這段代碼有什么問題?我添加了wp_reset_query();但問題仍然存在。

Thanks for help.

感謝幫助。

1 个解决方案

#1


Ok, I resolve the problem.

好的,我解決了這個問題。

I used WP_query class instead query_posts.

我使用WP_query類而不是query_posts。

 <section id="services" class="services-section">
        <div class="container">
            <div class="row">
                <div class="col-xs-12">
                    <h2 class="text-left">Services</h2>
                </div>
            </div>
            <?php $inner_query = new WP_Query( 'category_name=services' ); ?>
            <?php if ( $inner_query->have_posts() ) : while ( $inner_query->have_posts() ) : $inner_query->the_post(); ?>
            <div class="row box-service">
                <div class="col-sm-6">
                    <?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
                </div>
                <div class="col-sm-6">
                    <h3><?php the_title(); ?></h3>
                    <p class="intro"><?php echo get_the_content(); ?></p>
                    <a href="#">More</a>
                </div>
            </div>
            <?php endwhile; else: endif; wp_reset_postdata(); ?>

        </div>
    </section>

This is working good.

這很好用。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/04/47c4fec8951c448d8b56adcb5b2c1adb.html



 
  © 2014-2022 ITdaan.com