Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter davestein1

    (@davestein1)

    Oops! I forgot to code the link. Reposting.

    I have several long pages, and I’d like the search results to include the name/anchor tag.
    Eg, In the page where Relevanssi finds the Search term is section in example.com/thispage.html that is named with <id="hello"> or perhaps <a name="hello"> (old style).

    And the external link that would be returned by Relevanssi in Search result would be https://example.com/thispage.html#hello

    I’m a programmer, and am happy to write a filter if/as required.

    Thank you!`
    `

    Plugin Author Mikko Saari

    (@msaari)

    Happy programming, as this is something you need to do yourself. Relevanssi can’t help you out here, because Relevanssi doesn’t know which section of the page matches the search query. Relevanssi doesn’t know anything about the sections at all, Relevanssi just knows what words appear in the document, and how often – that’s all.

    So, what you need to do here is to modify your search results template so that for each post, you find out where the search terms (which you can get from get_search_query() or $wp_query->query_vars['s']) appear in the document (the post content is in $post->post_content), and then instead of the_permalink(), you need to use get_permalink() and then add the section ID to the URL.

    Modifying the URL is the trivial part, the tricky part will be figuring out the sections in the first place. Also, think of how those cases work where the search terms appear in multiple sections –?which one will you use?

    Thread Starter davestein1

    (@davestein1)

    Thank you for the fast, informed response Mikko.
    I understand the more compatible approach would be to have a post for each of my calendar entries, but this would turn dozens of pages into thousands of posts.

    Regarding the multiple sections…If the Search Results snippet returns multiple occurrences on the page, I wonder if I can map those onto an array of anchors I create.

    Best,

    Dave

    Plugin Author Mikko Saari

    (@msaari)

    Yes, you can definitely provide multiple links to the different sections of the post.

    If the sections are calendar entries, one way to simplify this would be to store the section contents in custom fields, so that it would be easy to check against that data to see which section has the search terms, instead of first splitting up the post content into sections.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search results with name/anchor tag’ is closed to new replies.