WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Make a block disappear if there are no posts in a category (3 posts)

  1. adorablepolak
    Member
    Posted 1 year ago #

    Hi everyone.

    I have a problem with my first wordpress website. So far I've managed to sort any problem out, but for this one I need help from you, experienced wp users.
    Well, below is a html block of my index.php file. What I need is an if statement to show the all the <div id="news"> if there is at least one post in the news category. In other words, if the category news does not have any post, the <div id="news"> should not be visible.

    <div id="news" class="section_white_bg clearfix news_excerpt">
      <h3 class="white_gradient_bg_heading">News</h3>
      <div class="white_gradient_bg_body clearfix">
        <ul class="news_lines">
          <?php if ($port = new WP_Query( array( 'post_type' => 'News', 'posts_per_page' => 3 ) );
    while ( $port->have_posts() ) : $port->the_post(); ?>
          <li> <a href="<?php the_permalink(); ?>" class="border_button">
            <h3 class="news_title">
              <?php the_title(); ?>
            </h3>
            <br />
            <?php the_excerpt(); ?>
            </a> </li>
          <?php endwhile; ?>
        </ul>
      </div>
    </div>

    Hope you can help me. This is driving me mad. I'm using Types for custom post types.

  2. vjpo
    Member
    Posted 1 year ago #

    <?php $port = new WP_Query( array( 'post_type' => 'News', 'posts_per_page' => 3 );
    if ( $port->have_posts() ) : ?>
      <div>
        <ul>
          <?php while ( $port->have_posts() ) : $port->the_post(); ?>
          <li><?php the_title(); ?></li>
          <?php endwhile; ?>
        </ul>
      </div>
    <?php endif; ?>

    Something like this, I guess.

  3. adorablepolak
    Member
    Posted 1 year ago #

    Wow, it worked like a charm.
    Thanks a lot vjpo.

Topic Closed

This topic has been closed to new replies.

About this Topic