Display message if no posts in Custom Post Type loop

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>

Read more here:: Display message if no posts in Custom Post Type loop

Leave a Reply

Your email address will not be published. Required fields are marked *