• Resolved harjeet singh

    (@harjeet-singh)


    After having a look at the codex for the loop i tried the same but it doesn’t display error message. I am just pasting the below mentioned code in single.php

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <!– If the post is in the category we want to exclude, we simply pass to the next post. –>
    <?php if (in_category(‘3’)) continue; ?>

    <div class=”post”>

    <h2>“><?php the_title(); ?></h2>

    <small><?php the_time(‘F jS, Y’); ?></small>

    <div class=”entry”>
    <?php the_content(); ?>
    </div>

    <p class=”postmetadata”>Posted in <?php the_category(‘, ‘); ?></p>
    </div> <!– closes the first div box –>

    <?php endwhile; else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>

    This message is not displaying

    Sorry, no posts matched your criteria.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The error message will only display if the loop finds no posts. The loop is finding posts but you are telling it to skip over some of the posts it finds. So the condition “no posts were found” is not true, since posts WERE found.

    You can set a counter and force the error message like this:
    (I added NEW --> in front of the changes I added.
    Remove NEW --> from the code you paste)
    — top of loop —

    NEW --> $post_count = 0;
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <!-- If the post is in the category we want to exclude, we simply pass to the next post. -->
    <?php if (in_category('3')) continue; ?>
    NEW --> $post_count++;  // add 1 to post count
    <div class="post">

    — bottom of loop —

    <?php endwhile; else: ?>
      <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>
    NEW --> <?php if $post_count > 0 { ?>
    NEW -->   <p>Sorry, no posts matched your criteria.</p>
    NEW --> <?php } ?>

    Thread Starter harjeet singh

    (@harjeet-singh)

    After applying the code provided by you i got this error.

    Parse error: parse error, expecting `'(” in C:\wamp\www\

    After modifying the code to

    <?php if ($post_count > 0) { ?>

    The error was not there but the error Sorry, no posts matched your criteria. was also not showing up.

    Thread Starter harjeet singh

    (@harjeet-singh)

    I guess the $post_count was supposed to be equal to 0 , like

    <?php if ($post_count == 0) { ?>

    It serves for me . I don’t have much knowledge of php so can explain much , but it has served my purpose.

    Thread Starter harjeet singh

    (@harjeet-singh)

    Here is the code . Others may find it helpful.

    <?php $post_count = 0; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <!– If the post is in the category we want to exclude, we simply pass to the next post. –>
    <?php if (in_category(‘3’)) continue; ?>
    <?php $post_count++; // add 1 to post count ?>
    <div class=”post”

    <h2>”><?php the_title(); ?></h2>

    <small><?php the_time(‘F jS, Y’); ?></small>

    <div class=”entry”>
    <?php the_content(); ?>
    </div>

    <p class=”postmetadata”>Posted in <?php the_category(‘, ‘); ?></p>
    </div> <!– closes the first div box –>

    <?php endwhile; else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>
    <?php if ($post_count == 0) { ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php } ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Excluding Categories .. doesnt display error if there are no more posts’ is closed to new replies.