WordPress.org

Ready to get started?Download WordPress

Forums

How to Show Portion of Page in Search Results (2 posts)

  1. njwebwiz
    Member
    Posted 4 years ago #

    I've built a site with about 100 pages (not posts) in a parent > child > grandchild hierarchy (vs. categories). Some pages contain quite a bit of content, with each section separated but a custom classed <div>. I'm trying to get search to display results in a certain way, but am bumping into a problem I hope someone can help me with.

    The original chunk of code was:

    <?php if (is_search())
           the_excerpt();
    else
           the_content([params here]);
    ?>

    The problem is that "the_excerpt" shows the *first* 55 words. Sometimes the content containing the search query is hundreds of words "down the page", so showing the excerpt would be confusing to the end user since he would not see his query word/phrase.

    But I can't just display the_content either, because it's too much and still wouldn't give prominence to the query word/phrase in context.

    My first thought was to look for a plugin that would essentially do this:
    1. Find page(s) containing query string
    2. Back up X characters (or maybe words) in page starting at location of query string (where 'X' is perhaps 100-200)
    3. Display content from "backed up" stuff, through query string, then another 100-200 characters

    But I realized that could lead to situations where it "backed up" too far or not enough. So now I'm wondering if there's a way to do #1 above, but then do "back up until you see the <div> with a specific classname", then show content from that point, through the query string, then another 100-200 characters.

    I realize this is a pretty specific (and possibly strange) request, but the way my client has her content loaded doesn't lend itself to easy searching and I'm trying to come up with a solution. Failing that, is there an effective and useful way to get Google sitesearch to work within a bunch of WP pages?

    Thanks in advance for any help or guidance.

  2. Josh Leuze
    Member
    Posted 4 years ago #

    Search is a weak point for WordPress, but there are some good plugins trying to improve it, have you tried some of them?

    Search Unleashed specifically says that it does "smart highlighting that shows a contextual snapshot of the search data".

    It looks like it builds and index of all your site's content and searches from that. From the screenshots it seems to highlights search terms and loads a relevant excerpt from the content, so that plugin might be a good place to start.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.