用于页面的另一个循环内的帖子的 Wordpress 循环 - 一页网站
Wordpress loop for posts inside another loop for pages - one page website
我在 WordPress 中制作了着陆页(一页)。
我在 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; ?>
我想在页面服务中显示来自类别服务的帖子。
服务-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>
页面服务显示服务类别的所有帖子,但在服务页面下,wordpress 循环再次显示服务类别的帖子,并且删除了联系部分。
这段代码有什么问题?我添加 wp_reset_query();但问题依然存在。
感谢您的帮助。
好的,我解决了问题。
我用 WP_query class 代替 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>
效果很好。
我在 WordPress 中制作了着陆页(一页)。
我在 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; ?>
我想在页面服务中显示来自类别服务的帖子。
服务-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>
页面服务显示服务类别的所有帖子,但在服务页面下,wordpress 循环再次显示服务类别的帖子,并且删除了联系部分。
这段代码有什么问题?我添加 wp_reset_query();但问题依然存在。
感谢您的帮助。
好的,我解决了问题。
我用 WP_query class 代替 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>
效果很好。