dsmupo6631 2015-05-04 19:45
浏览 37
已采纳

用于页面的另一个循环内的帖子的Wordpress循环 - 一个页面网站

I made landing page(onepage) in WordPress.

I display all pages in wordpress loop.

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.

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

Thanks for help.

  • 写回答

1条回答 默认 最新

  • douqi0090 2015-05-04 20:07
    关注

    Ok, I resolve the problem.

    I used WP_query class instead 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果