I am trying to get this thing to work, but can't figure out where is the problem. I added else statement, but it does not work. I was able to get this work with jQuery, but I need it to work in PHP as I will need to show this message in different languages. Can somebody help me?
Here is the code I have now:
<?php
$args = array( 'post_type' => 'custom_jobs', 'posts_per_page' => 30, 'order' => 'ASC' );
$loop = new WP_Query( $args );
if( $loop ):
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="accordion-container" id="accordion-container">
<div class="accordion" id="accordionJobs<?php the_ID(); ?>">
<div class="card">
<div class="card-header" id="heading<?php the_ID(); ?>">
<h2 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse<?php the_ID(); ?>" aria-expanded="false" aria-controls="collapse<?php the_ID(); ?>">
<h5><?php the_title(); ?></h5>
<i class="fas fa-chevron-right"></i>
</button>
</h2>
</div>
<div id="collapse<?php the_ID(); ?>" class="collapse" aria-labelledby="heading<?php the_ID(); ?>" data-parent="#accordionJobs<?php the_ID(); ?>">
<div class="card-body"><?php the_content(); ?></div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<h1>There are no posts!</h1>
<?php endif; ?>
</div>
</div>
I am trying to get this thing to work, but can't figure out where is the problem. I added else statement, but it does not work. I was able to get this work with jQuery, but I need it to work in PHP as I will need to show this message in different languages. Can somebody help me?
Here is the code I have now:
<?php
$args = array( 'post_type' => 'custom_jobs', 'posts_per_page' => 30, 'order' => 'ASC' );
$loop = new WP_Query( $args );
if( $loop ):
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="accordion-container" id="accordion-container">
<div class="accordion" id="accordionJobs<?php the_ID(); ?>">
<div class="card">
<div class="card-header" id="heading<?php the_ID(); ?>">
<h2 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse<?php the_ID(); ?>" aria-expanded="false" aria-controls="collapse<?php the_ID(); ?>">
<h5><?php the_title(); ?></h5>
<i class="fas fa-chevron-right"></i>
</button>
</h2>
</div>
<div id="collapse<?php the_ID(); ?>" class="collapse" aria-labelledby="heading<?php the_ID(); ?>" data-parent="#accordionJobs<?php the_ID(); ?>">
<div class="card-body"><?php the_content(); ?></div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<h1>There are no posts!</h1>
<?php endif; ?>
</div>
</div>
Share
Improve this question
asked Feb 10, 2020 at 7:11
TheRoyalityTheRoyality
136 bronze badges
1 Answer
Reset to default 2It's because you're only checking if ( $loop ) {
.
$loop
isn't empty, it's a full WP_Query
object with all the information about the query in it, even if no posts were returned. To check if the query actually returned any posts you need to use $loop->have_posts();
:
$loop = new WP_Query( $args );
if ( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post();
endwhile;
else:
endif;