0

I have a basic search function running on my website.
This works fine. However I have only 5 pages.
So the search function will never be longer then 5 pages which could be a good thing.
The pages are insanely long. So if youre searched word is on a page you never know where.
This kind of makes the search function useless. So I was wondering if there is way to make the searched word into a link with an anchor of some sort.

I understand that the problem here would be to create anchor points on every word on the page. (This would be insane).

I know I can show the searched word in the results with this function:

function search_title_highlight() {
$title = get_the_title();
$keys = implode('|', explode(' ', get_search_query()));
$title = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $title);

echo $title;
}

function search_content_highlight() {
    $content = get_the_content();
    $keys = implode('|', explode(' ', get_search_query()));
    $content = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $content);

    echo '<p>' . $content . '</p>';
}

function search_excerpt_highlight() {
$excerpt = get_the_excerpt();
$keys = implode('|', explode(' ', get_search_query()));
$excerpt = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $excerpt);

echo '<p>' . $excerpt . '</p>';
} 

Where this is my search function:

<?php
            $search_count = 0;

            $search = new WP_Query("s=$s & showposts=-1");
            $exclude_option = get_option('ep_exclude_pages');
            if($search->have_posts()) : while($search->have_posts()) : $search->the_post(); if($exclude_option) continue;
            $search_count++;
            endwhile; endif;

            echo '<p style="color:#271d67; margin: -20px 0 15px;">'. $search_count . ' results for ' . '&lsquo;'.$s.'&rsquo;' . '</p>';
            ?> 
            <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
            <h2 class="h2_link"><a href="<?php the_permalink(); ?>"><?php search_title_highlight(); ?></a></h2>
            <?php the_excerpt(); ?>
                <p class="postmetadata">
                    URL: <a href="<?php the_permalink(); ?>"><?php the_permalink(); ?></a>
                </p>
                <hr />
            <?php endwhile; ?>
            <?php else : ?>
            <?php endif; ?>

I have no idea how to make this linkable to the content in the page. Maybe with some sort of $_GET[] command in the content page?

M.

0

you could include the search term as a parameter in the URL that links to the page

http://www.example.com/page-here/?search-highlight=frogs

and then hook into the the_content filter to replace all instances of the search-highlight term with a highlighted version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.