Custom wp_query inside a conditional stament inside a template part doesn't work: why?

this is a bit messy and I cannot understand why my approach isn't working or what my error is.

I'm using a template part to include a custom wp_query into my theme. I'd like to have two different behaviours throughout the theme, so I'm using a conditional statement inside the template part to output two different loops.

In my front-page.php I call the template part with get_template_part() using the $args option:

?php get_template_part('template-parts/modules/moduli-offerta/offerta-figli','lista', array('tipologia' = 'lista') ); ?

In my template part I have this code:

?php if ( $args['tipologia'] == 'lista' ) : ?
    ...
?php elseif ( $args['tipologia'] == 'descrizione' ) : ?
    ...
?php else : ?
    ...
?php endif; ?

This code works: if I put any text or HTML5 inside the conditional statement, it shows the output correctly depending on what tipologia I choose to adopt.

But if I put a custom loop (via wp_query) inside the if statement, the output is blank.

It's like the if statement of wp_query inside the initial if statement break something.

Can you help me understand? Hint: the custom loop works if I DON'T put it inside a conditional statement.

Here is the full code:

?php if ( $args['tipologia'] == 'lista' ) : ? 

!-- First Loop Option --

   ?php $cm_offerta_post_figli = new WP_Query( array( 
      'post_type' = 'cm_offerta', 
      'post_parent' =  get_the_ID(),
      'order' = 'ASC',
      )
   ); ?

   ?php if ( $cm_offerta_post_figli-have_posts() ) : ?
      ul class=list-group list-group-flush ms-0
         ?php while ( $cm_offerta_post_figli-have_posts() ) : ?
         ?php $cm_offerta_post_figli-the_post(); ?
         li class=list-group-item?php the_title(); ?/li
         ?php endwhile; ?
      /ul
   ?php endif; ?  

!-- END of First Loop Option --

?php elseif ( $args['tipologia'] == 'descrizione' ) : ?

!-- Second Loop Option --

   ?php if ( $cm_offerta_post_figli-have_posts() ) : ?
         div class=bg-light row row-cols-1 row-cols-md-2 mb-5 p-5
            ?php while ( $cm_offerta_post_figli-have_posts() ) : ?
            ?php $cm_offerta_post_figli-the_post(); ?
            div class=col
              div class=card bg-transparent border-0
                div class=card-body
                   h3 class=card-title?php the_title(); ?/h3
                   ?php the_content(); ?
                /div
              /div    
            /div
            ?php endwhile; ?
         /div
   ?php endif; ?

!-- END of Second Loop Option --

?php else : ?
    Errore
?php endif; ?

?php wp_reset_query(); ?

Topic get-template-part loop wp-query conditional-content Wordpress

Category Web


Firstly, please check the below query either it is resulting anything.

<?php $cm_offerta_post_figli = new WP_Query( array( 
      'post_type' => 'cm_offerta', 
      'post_parent' =>  get_the_ID(),
      'order' => 'ASC',
      )
   ); ?>

If not please check this one only. You will definitely get the solution.


You have assigned the queries result inside the first condition. So the values assigned to this variable "cm_offerta_post_figli" won't be accessible inside the second elseif condition.

Please put the below code on the top of First condition. It will work.

<?php $cm_offerta_post_figli = new WP_Query( array( 
  'post_type' => 'cm_offerta', 
  'post_parent' =>  get_the_ID(),
  'order' => 'ASC',
  )
); ?>

I have modified your code as below, perhaps it will help:

<?php $cm_offerta_post_figli = new WP_Query( array( 
  'post_type' => 'cm_offerta', 
  'post_parent' =>  get_the_ID(),
  'order' => 'ASC',
  )
); ?>

<?php if ( $args['tipologia'] == 'lista' ) : ?> 

<!-- First Loop Option -->

   <?php if ( $cm_offerta_post_figli->have_posts() ) : ?>
      <ul class="list-group list-group-flush ms-0">
         <?php while ( $cm_offerta_post_figli->have_posts() ) : ?>
         <?php $cm_offerta_post_figli->the_post(); ?>
         <li class="list-group-item"><?php the_title(); ?></li>
         <?php endwhile; ?>
      </ul>
   <?php endif; ?>  

<!-- END of First Loop Option -->

<?php elseif ( $args['tipologia'] == 'descrizione' ) : ?>

<!-- Second Loop Option -->

   <?php if ( $cm_offerta_post_figli->have_posts() ) : ?>
         <div class="bg-light row row-cols-1 row-cols-md-2 mb-5 p-5">
            <?php while ( $cm_offerta_post_figli->have_posts() ) : ?>
            <?php $cm_offerta_post_figli->the_post(); ?>
            <div class="col">
              <div class="card bg-transparent border-0">
                <div class="card-body">
                   <h3 class="card-title"><?php the_title(); ?></h3>
                   <?php the_content(); ?>
                </div>
              </div>    
            </div>
            <?php endwhile; ?>
         </div>
   <?php endif; ?>

<!-- END of Second Loop Option -->

<?php else : ?>
    Errore
<?php endif; ?>

<?php wp_reset_query(); ?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.