Wordpress多个循环不在页面上显示

时间:2015-06-24 19:00:49

标签: php wordpress while-loop custom-post-type custom-taxonomy

我正在尝试在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>

1 个答案:

答案 0 :(得分:0)

好的,我检查你的代码删除

function dd_services_template(){

<?php wp_reset_postdata(); } ?>

它工作正常。只需检查您是否正确调用该功能。

顺便说一下:我想你必须在最后一个div标签之后关闭这个功能。