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(); ?>
$cm_offerta_post_figli
is only defined if$args['tipologia']
islista
. Also, you shouldn't need to callwp_reset_query()
, and instead, you should callwp_reset_postdata()
after theendwhile
orendif
for your loops.$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.$cm_offerta_post_figli = new WP_Query( ... );
to above theif ( $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?$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.