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(); ?>