Home Add a custom message when no custom posts in a loop
Reply: 1

Add a custom message when no custom posts in a loop

fionchadd
1#
fionchadd Published in 2018-02-13 10:03:39Z

I have a custom post type on a site and I want to display a message when there aren't any items of that post type.

I've tinkered around with the php and looked at a few tutorials but the syntax in the tutorials is slightly different to what I'm using so I can't quite get it to work.

This is what I have so far:

<section class="bigsection clearfix" id="joinus">

  <h2><?php echo(types_render_field( 'job-section-header', array( ) )); ?></h2>
  <p class="bodycopy"><?php echo(types_render_field( 'job-section-text', array( ) )); ?></p>
  <div class="jobs">
  <?php

        // WP_Query arguments
        $args = array (
            'post_type'     => array( 'job' ),
            'nopaging'      => true,
            'orderby'       => 'menu_order',
            'order'         => 'ASC'
        );

        $jobs = new WP_Query( $args );

        if ( $jobs->have_posts() ) {


            while ( $jobs->have_posts() ) {
                $jobs->the_post();
                            ?>

      <div class="single-job">
      <h3><?php the_title( ); ?></h3>
      <p><a href="<?php echo(types_render_field( 'link-to-job-description', array('output' => 'raw' ) )); ?>">Job Specification</a></p>
      </div>
  </div>
endwhile;
}
else {
            <p>There are no current vacancies at this time</p>
          }
        ?>
<?php
            }

        }

        // Restore original Post Data
        wp_reset_postdata();

        ?>

</section>

The code is apparently technically correct (i.e. no syntax errors) but while it's correctly displaying available jobs, it's not showing the 'There are no current vacancies at this time' message when there aren't any jobs available.

Any help would be greatly appreciated!

Thanks :)

Alex Vand
2#
Alex Vand Reply to 2018-02-13 10:18:55Z

You have too much brackets, and your endwhile has not <?php tag before it to make it works. You ".jobs" div also needs to be in the if condition. It's also better to add the wp_reset_postdata(); just after the endwhile, and not after all the condition, because if you don't enter the if condition, you do not need to use wp_reset_postdata();. And remove all the brackets because when you have a lot of html you can't really see them easily.

<section class="bigsection clearfix" id="joinus">

  <h2><?php echo(types_render_field( 'job-section-header', array( ) )); ?></h2>
  <p class="bodycopy"><?php echo(types_render_field( 'job-section-text', array( ) )); ?></p>
  <?php

    // WP_Query arguments
    $args = array (
        'post_type'     => array( 'job' ),
        'nopaging'      => true,
        'orderby'       => 'menu_order',
        'order'         => 'ASC'
    );

    $jobs = new WP_Query( $args );

    if ( $jobs->have_posts() ) : ?>

      <div class="jobs">
        <?php while ( $jobs->have_posts() ) : $jobs->the_post();?>

            <div class="single-job">
                <h3><?php the_title( ); ?></h3>
                <p><a href="<?php echo(types_render_field( 'link-to-job-description', array('output' => 'raw' ) )); ?>">Job Specification</a></p>
            </div>

        <?php endwhile; wp_reset_postdata();?>
      </div>
    <?php else : ?>
        <p>There are no current vacancies at this time</p>
    <?php endif; ?>


</section>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.351817 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO