Wordpress Multiple Loops Not Displaying on Page

58 views Asked by At

I am trying to run multiple loops on a Wordpress page to display the different posts based on their category. I am using Custom Post Types and a Custom Taxonomy. Can someone let me know what I am missing in my code below? Also, is there a better way to do this?

<?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

There are 1 answers

9
Clyff On

Ok i check your code removing

function dd_services_template(){

and

<?php wp_reset_postdata(); } ?>

And it's working fine. Just check if you are calling the function correctly.

Btw: i guess you have to close the function after the last div tag.