kwek-kwek
kwek-kwek

Reputation: 1343

Wordpress search results

I have created a searchfrom.php for wordpress but then it's giving me a false returns you can try the search yourself here

Here is the code for my search form

<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<input type="text" class="form-text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" class="goField" />
</form>

Here is the code of my search.php page

get_header(); ?>
  <div id="BodyWrap">
<div id="mainCont">
<?php get_sidebar(); ?>
<div id="mainCopy">

    <div id="content" class="narrowcolumn" role="main">

    <?php if (have_posts()) : ?>

        <h2 class="pagetitle">Search Results</h2>

        <div class="navigation">
            <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
            <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
        </div>


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

            <div <?php post_class() ?>>
                <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                <small><?php the_time('l, F jS, Y') ?></small>

                <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
            </div>

        <?php endwhile; ?>

        <div class="navigation">
            <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
            <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
        </div>

    <?php else : ?>

        <h2 class="center">No posts found. Try a different search?</h2>
        <?php get_search_form(); ?>

    <?php endif; ?>

    </div>
</div>
</div>
</div>
<?php get_footer(); ?>

It does do the search but there are this "at | Uncategorized | No comment" which is not even part of the search term.

Upvotes: 1

Views: 1283

Answers (4)

Michal Kopanski
Michal Kopanski

Reputation: 353

Remove the line that's wrapped in

. That will get rid of the "Posted in Uncategorized | Comments Off".

The reason it show's is because it is the meta information for your search result. It will be displayed in your results even if it wasn't the search term because it is associated with it.

All in all, your search.php page should look like this:

get_header(); ?>
<div id="BodyWrap">
<div id="mainCont">
<?php get_sidebar(); ?>
<div id="mainCopy">

    <div id="content" class="narrowcolumn" role="main">

    <?php if (have_posts()) : ?>

        <h2 class="pagetitle">Search Results</h2>

        <div class="navigation">
            <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
            <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
        </div>


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

            <div <?php post_class() ?>>
                <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                <small><?php the_time('l, F jS, Y') ?></small>

            </div>

        <?php endwhile; ?>

        <div class="navigation">
            <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
            <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
        </div>

    <?php else : ?>

        <h2 class="center">No posts found. Try a different search?</h2>
        <?php get_search_form(); ?>

    <?php endif; ?>

    </div>
</div>
</div>
</div>
<?php get_footer(); ?>

Upvotes: 0

jocap
jocap

Reputation: 476

Oh, I see. You want to know why it lists categories even if they don't match the search term? Well, that's the default WordPress behavior. I guess you could change it, but I don't see why, actually.

It is the


Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>

part in your search.php that displays the categories and comments count. However, if you remove the code above, the categories and comments count will not be shown (even if the categories matches your search term).

You may replace the code above with this code, if you only want to display the categories if they are the same as the search term:


<?php
foreach(get_the_category() as $cat){
    if(strtolower($cat->cat_name)==strtolower($_GET['s'])){
        $match = true;
    }
}
if($match){ echo 'Posted in '; }
foreach(get_the_category() as $cat){
    if(strtolower($cat->cat_name)==strtolower($_GET['s'])){
        echo $cat->cat_name;
        echo ', ';
    }
}
edit_post_link('Edit', '', ' | ');
comments_popup_link('No Comments »', '1 Comment »', '% Comments »');
?>
It should produce something like this, if the categories match the search term:

Posted in Category That Matches, Edit | 1 Comment

If the categories don't match the term:

Edit | 1 Comment

If you don't want the comments count (or Comments Off) to display, remove the comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); line from my code.

Upvotes: 1

Michal Kopanski
Michal Kopanski

Reputation: 353

Maybe change searchform.php to this?

Upvotes: 0

Art W
Art W

Reputation: 2038

It does do the search but there are this "at | Uncategorized | No comment" which is not even part of the search term.

You'll have to edit your page.php or single.php to exclude the search page from showing the Comments/Categories.

Upvotes: 0

Related Questions