• Resolved adorablepolak

    (@adorablepolak)


    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • <?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.

    Thread Starter adorablepolak

    (@adorablepolak)

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Make a block disappear if there are no posts in a category’ is closed to new replies.