【发布时间】:2015-09-11 03:27:16
【问题描述】:
我正在尝试在 Wordpress 页面上运行多个循环,以根据类别显示不同的帖子。我正在使用自定义帖子类型和自定义分类法。有人可以让我知道我在下面的代码中缺少什么吗?另外,有没有更好的方法来做到这一点?
<?php function dd_services_template() {
$first_query = new WP_Query( array('post_type' => 'services', 'posts_per_page' => -1, 'category_name' => 'web') );
$second_query = new WP_Query( array('post_type' => 'services', 'posts_per_page' => -1, 'category_name' => 'brand') );
$third_query = new WP_Query( array('post_type' => 'services', 'posts_per_page' => -1, 'category_name' => 'print') );
$fourth_query = new WP_Query( array('post_type' => 'services', 'posts_per_page' => -1, 'category_name' => 'media') );
?>
<div class="services">
<ul class="servicesul">
<?php
if ( $first_query->have_posts() ) {
while ( $first_query->have_posts() ) : $first_query->the_post(); ?>
<li>
<div class="servicestitle"><?php the_title() ?></div>
<div class="servicescontent"><?php the_content(); ?></div>
</li>
<?php endwhile; } ?>
<?php
if ( $second_query->have_posts() ) {
while ( $second_query->have_posts() ) : $second_query->the_post(); ?>
<li>
<div class="servicestitle"><?php the_title() ?></div>
<div class="servicescontent"><?php the_content(); ?></div>
</li>
<?php endwhile; } ?>
<?php
if ( $third_query->have_posts() ) {
while ( $third_query->have_posts() ) : $third_query->the_post(); ?>
<li>
<div class="servicestitle"><?php the_title() ?></div>
<div class="servicescontent"><?php the_content(); ?></div>
</li>
<?php endwhile; } ?>
<?php
if ( $fourth_query->have_posts() ) {
while ( $fourth_query->have_posts() ) : $fourth_query->the_post(); ?>
<li>
<div class="servicestitle"><?php the_title() ?></div>
<div class="servicescontent"><?php the_content(); ?></div>
</li>
<?php endwhile; } ?>
<?php wp_reset_postdata(); } ?>
</ul>
</div>
【问题讨论】:
-
那么......你到底要返回什么?带有空ul的html?任何查询都有效吗?它们都返回空(找到 0 个值)?
-
@Clyff 是的,我唯一得到的是
<div class="services"> <ul class="servicesul"> </ul> </div>...所有查询都不起作用。
标签: php wordpress while-loop custom-post-type custom-taxonomy