Support » How-To and Troubleshooting » Check if any published posts exist?

Check if any published posts exist?

  • 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:

      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)
  • Michael


    Forum Moderator

    i would think that if ( have_posts() ) : does this already.

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

    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?



    Forum Moderator

    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.

    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.