WordPress.org

Ready to get started?Download WordPress

Forums

Check if any published posts exist? (5 posts)

  1. jessn
    Member
    Posted 4 years ago #

    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!

  2. alchymyth
    Forum Moderator
    Posted 4 years ago #

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

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

  3. jessn
    Member
    Posted 4 years ago #

    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?

  4. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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.

  5. jessn
    Member
    Posted 4 years ago #

    Yep! That did it. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic