WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] conditional statements when category has no posts display message (4 posts)

  1. oldbag66
    Member
    Posted 4 years ago #

    Hi, i want to add a message so if a catagory doesnt have any posts a message is display saying so, "sorry no posts".

    Im am using a list of conditional staments for a sidebar and would like to use it here:

    <ul class="rightcol">
    <?php global $post; $myposts = get_posts('numberposts=10&amp;category=18'); foreach($myposts as $post) :?>
    <li><span class="threecolsmall"><?php the_time('j M y') ?></span><br />
    <a href='<?php the_permalink() ?>'><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>

    not sure how to add this into the PHP.

  2. stvwlf
    Member
    Posted 4 years ago #

    Hi
    try this code

    <ul class="rightcol">
    <?php
    $myposts = new WP_Query();
    $myposts->query('showposts=10&amp;cat=18');
    
    if ($myposts->post_count == 0) { ?>
       <li>sorry no posts</li>
    <?php } else {
      while ($myposts->have_posts()) : $myposts->the_post(); ?>
        <li><span class="threecolsmall"><?php the_time('j M y') ?></span><br />
        <a href='<?php the_permalink() ?>'><?php the_title(); ?></a></li>
      <?php endwhile;
    } ?>
    </ul>
  3. oldbag66
    Member
    Posted 4 years ago #

    perfect, thankyou.

  4. Zeal Murapa
    Member
    Posted 4 years ago #

    HEELP!

    Hi, i want to add a message so if a 'parent page' link doesnt have any 'subpages' a message is display saying so, "sorry no subpages".

Topic Closed

This topic has been closed to new replies.

About this Topic