0

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(); ?>
6
  • 2
    From what I could see, the problem happened most likely because $cm_offerta_post_figli is only defined if $args['tipologia'] is lista. Also, you shouldn't need to call wp_reset_query(), and instead, you should call wp_reset_postdata() after the endwhile or endif for your loops.
    – Sally CJ
    Jul 14 '21 at 5:26
  • Also, these sorts of issues are much easier to solve if you indent your code properly. Jul 14 '21 at 6:35
  • @SallyCJ: I moved the definition of $cm_offerta_post_figli up and down the code, trying to understand what the problem might be. In the first iteration, the definition was before the if statement with $args['tipologia']... It didn't work the same. @JacobPeattie: I'm sorry, indenting is not my forte but here on StackExchange I had to realign all my code for it to be displayed properly. I'll try and edit in a better, more understandable way.
    – Gatto Nero
    Jul 14 '21 at 8:44
  • @GattoNero, you just needed to move the $cm_offerta_post_figli = new WP_Query( ... ); to above the if ( $args['tipologia'] == 'lista' ) : part, just as what the first answer stated and shown. But if that didn't solve the problem, then have you tried clearing your caches? Or have you now already solved the problem; if so, how did you solve it?
    – Sally CJ
    Jul 18 '21 at 7:30
  • @SallyCJ: no, moving the $cm_offerta_post_figli = new WP_Query( ... ); above the if statement didn't work. By the way, it was my original code (before I tried different solutions, and then I posted the tweaked code above. I have no idea how to solve the problem because I can't understand what the problem is. That's why I went on a different route and created two different template parts to call in different part of my theme.
    – Gatto Nero
    Jul 24 '21 at 5:15
0

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(); ?>
1
  • Thank you @Deepak... Sadly, even moving the new query declaration up, I still not have an output. I really don't understand why.
    – Gatto Nero
    Jul 14 '21 at 14:07
0

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.

1
  • It shows what it has to show. Used standalone, the query works, 100%. But if I put it inside a conditional statement with the $args passed by get_template_part, it doesn't work.
    – Gatto Nero
    Jul 15 '21 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.