Forums

[resolved] Conditional Tag not working (must show text if no posts) (3 posts)

  1. Reaxion
    Member
    Posted 5 months ago #

    I've seen questioning posts like this before and not all are resolved, but I'll take a crack at asking!

    I would like certain text (or eventually certain html) to show when there are no posts in a category. This will eventually be for listing products, and if there are no products, a greyed out icon will show.

    Anyway, this is the code I have now and I'm at a loss as to why it won't work (though I'm more of a designer than php guru). If anything looks out of the ordinary, realize I'm using the "magic fields" plugin.

    The Code:

    <?php $my_query = new WP_Query('category_name=product-category'); ?>

    <?php if (have_posts()) : ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div>
    See this if there are posts in the category
    </div>

    <?php endwhile; ?>
    <?php else : ?>
    <p>See this if there are no posts in the category</p>
    <?php endif; ?>

    ----------------------

    To me the above is logical and should work. It DOES show text if there is a post in the category and the text DOES disappear if there are no posts, I just can't get the "no post stuff" to appear when there are no posts!

    Thanks in advance!

  2. kionae
    Member
    Posted 5 months ago #

    Try replacing <?php if (have_posts()) : ?> with <?php if ($my_query->have_posts()) : ?>

  3. Reaxion
    Member
    Posted 5 months ago #

    Thanks!

    I honestly wouldn't have touched that part of the code as I thought it was solid, ha ha!

    Thanks again!

Reply

You must log in to post.

About this Topic