• care2michelle

    (@care2michelle)


    I’ve used the Knowledge Base article Scroll results page to search term.

    I’ve tried the “easy” way listed in the article and the other option listed in the comments to Levi.

    My search terms are highlighted on the initial results page, but when you click on a result, it opens and defaults to the top of the page, so the user has to scroll down to attempt to locate what was actually returned in the search results.

    This is the code I currently am trying (it’s the one from the comments to Levi)

    `<?php
    add_action( ‘wp_footer’, function() {
    ?>
    <script>
    jQuery(document).ready(function($) {
    var offset = $(‘mark’).first().offset().top // use this line if you use <mark> tags
    $(“html, body”).animate({ scrollTop: offset }, 100);
    })
    </script>
    <?php
    } );

    I am using the free version. Thank you!

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Mikko Saari

    (@msaari)

    First, there’s no highlight parameter in the links on the search results page. Thus there are no highlights on the post page and no scrolling to the highlights. Make sure “Highlight query terms in documents” is checked in the Relevanssi excerpt settings.

    Second, the browser Javascript console has this error:

    Uncaught ReferenceError: jQuery is not defined

    <?php
    add_action( 'wp_footer', function() {
    ?>
    <script>
    $(document).ready(function($) {
    var offset = $('mark').first().offset().top
    $("html, body").animate({ scrollTop: offset }, 100);
    })
    </script>
    <?php
    } );

    Perhaps this version works better? Try visiting this page on your site, for example: /general-grief-websites/?highlight=keepsake

    Thread Starter care2michelle

    (@care2michelle)

    Thank you for your quick reply. The box for “highlight query terms in documents” is checked. All the boxes in the “Search Hit Highlighting” section are checked. The “Highlight Type” is set to <mark>.

    I’ve tried adding the new code you pasted above, but it’s still not work.

    I really love the plugin, and I’m grateful for your help as I try to get this sorted. Thank you!

    Thread Starter care2michelle

    (@care2michelle)

    I’m wondering if the problem is because I don’t have full posts, but I’m using the post as the place where I’m embedding a pdf. Could that be the reason? And the regular pages are not set up as posts either. Will it not be able to scroll to content unless the content is contained in a custom excerpt?

    Plugin Author Mikko Saari

    (@msaari)

    The type of your posts does not matter. The code doesn’t know or care.

    I now see an Uncaught ReferenceError: $ is not defined error. It looks like you don’t have jQuery on your site. That complicates things a bit: you can’t use jQuery-based highlighting code when you don’t have jQuery. You’d need a scrolling script that does the same thing as the above but without jQuery. This might work:

    <?php
    add_action( 'wp_footer', function() {
      ?>
    <script>
      document.getElementsByTagName("mark")[1].scrollIntoView();
    </script>
      <?php
    } );

    However, the highlight parameter is still not showing up in your links. What is your search results template using to print out the permalinks to the posts? It may be something custom that skips the Relevanssi additions.

    With the script above, this page should scroll to position: /grief-what-to-expect/?highlight=mourning

    Thread Starter care2michelle

    (@care2michelle)

    I’m using the WordPress 2022 default theme. The search templates page looks like it’s just pulling a Query Loop that returns the post title, excerpt, and date. I’m sorry, I don’t really understand the coding aspect, so I’m not sure where to look to see what code is behind all that. If you tell me where to look, I can find it.

    Plugin Author Mikko Saari

    (@msaari)

    Okay, that’s helpful. I tried using the Twenty Twenty-Two theme, and the highlight parameters don’t appear there.

    I’m sorry, but you’re out of luck for now. I don’t know a way to fix this at the moment. I’ll look into this and see if I can make the highlight parameter appear in the links, but for now, this is one of the many restrictions that using a full-site editing theme will incur. It’s a new approach to WP theming that changes many things; this is one of those.

    Thread Starter care2michelle

    (@care2michelle)

    That’s actually really helpful, my first assumption is that I’m doing something wrong. If you’re able to come up with a solution, I’m happy to implement it. I still love the plugin and plan to keep using it. I appreciate your time and assistance!

    Plugin Author Mikko Saari

    (@msaari)

    The next version of Relevanssi will solve this problem by introducing a new filter hook, relevanssi_add_highlight_and_tracking. This hook can force Relevanssi to add the highlight and tracking parameters. The simplest solution is this:

    add_filter( 'relevanssi_add_highlight_and_tracking', '__return_true' );

    This will make sure all links have the highlight parameter. This may cause the parameter to appear in unwanted links; more finesse may be required, but at least now it will be possible to get the parameter in the links.

    Thread Starter care2michelle

    (@care2michelle)

    That’s great! Thank you so much! when will the next version be released? In the meantime, should I add the code you noted to the theme’s php?

    Plugin Author Mikko Saari

    (@msaari)

    Hopefully, this week. You can add the code; it won’t do anything until the new version rolls out.

    Thread Starter care2michelle

    (@care2michelle)

    Perfect, thank you so much!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Scroll results page to search term’ is closed to new replies.