• I’m using a custom post type on a page, but I only want to display posts (and all the extra content on that page) if any exist. I’m not sure how to write “check if there are any posts. If so, show all this stuff [code], else don't show anything"

    Does anyone know how to do this?

    Here's what I have right now:

    <?php
      query_posts( array( 'events' => 'recurring-event','showposts' => $postsperpage,'paged'=>$paged  ) ); ?>
    
     <? if ( have_posts() ) :  while ( have_posts() ) : the_post();  ?>

    Thanks for any assitance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • i would think that if ( have_posts() ) : does this already.

    (your above code does not show anything on the html side in the browser)

    Thread Starter Jess

    (@jessn)

    Yeah it does, but the problem is before the if (have_posts() ) I have an upper area, hard coded in, like:

    <h2>Title of the area</h2>
    <p>something something something</p>
    ..if have posts stuff here….

    And I don’t want that above area to show if there are no posts…. And I could put that down inside the if have posts area, but then it would repeat for every post, wouldn’t it?

    pull your hard coded upper area to the space after the if(have_posts()): and before the while(have_posts()):

    there, it will only be shown once, if there are posts.

    Thread Starter Jess

    (@jessn)

    Yep! That did it. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Check if any published posts exist?’ is closed to new replies.