WordPress.org

Ready to get started?Download WordPress

Forums

Search results not showing the correct number (3 posts)

  1. timtastic
    Member
    Posted 7 years ago #

    I'm having a problem with displaying the number of my search results:

    http://alexflint.net/tag/hummer

    If you visit the link, you'll see "2 results found", when actually there are more than 2. You can navigate to the previous entries and see more results.

    It seems to do this whenever more than 2 results are found. Can anyone fix my search.php code?

    <?php get_header(); ?>
    <div id="primary" class="single-post">
      <div class="inside">
        <div class="primary">
          <?php if (have_posts()) : ?>
          <h1>Search Results</h1>
          <ul class="dates">
            <?php while (have_posts()) : the_post(); ?>
            <li> <span class="date">
              <?php the_time('n.j.y') ?>
              </span> <a href="<?php the_permalink() ?>">
              <?php the_title(); ?>
              </a> |
              <?php the_category(', ') ?>
    <p />
    <?php the_excerpt_reloaded(50, 'all', 'content', FALSE); ?>
            </li>
            <?php $results++; ?>
            <?php endwhile; ?>
          </ul>
          <div class="navigation">
            <div class="left">
              <?php next_posts_link('&laquo; Previous Entries') ?>
            </div>
            <div class="right">
              <?php previous_posts_link('Next Entries &raquo;') ?>
            </div>
          </div>
          <?php else : ?>
          <h1>No posts found. Try a different search?</h1>
          <?php endif; ?>
        </div>
        <div class="secondary">
          <h2>Search</h2>
          <div class="featured">
            <p>You searched for &ldquo;<?php echo wp_specialchars($s, 1); ?>&rdquo;
              at
              <?php bloginfo('name'); ?>
              . There were
              <?php
    				if (!$results) echo "no results, better luck next time.";
    				elseif (1 == $results) echo "one result found. It must be your lucky day.";
    				else echo $results . " results found.";
    			?>
            </p>
          </div>
        </div>
        <div class="clear"></div>
      </div>
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  2. topdownjimmy
    Member
    Posted 6 years ago #

    The crucial line here is <?php $results++; ?>

    This just increments a PHP variable for every post displayed on the current page (which you apparently have set to a limit of 2). It can't count the posts on other pages.

    I think in order to display the number of search results accurately, you need to install a plugin.

  3. whooami
    Member
    Posted 6 years ago #

    dont need a plugin to do that, just need a little php magic:

    http://www.village-idiot.org/search/jkhkjlhkjhkhkh (0)

    http://www.village-idiot.org/search/christmas (lots)

    http://www.village-idiot.org/search/rootlab (just one)

    check the titles of the pages.

    it's magic, I tell ya!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags