• Resolved sm2dev

    (@kelg7)


    Once a search query is submitted and the results page loads, is it possible to have the page scroll to the top of the results div?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Kris

    (@c0nst)

    Hi @kelg7!

    The solution is in the code snippet below.

    You have two ways to add this code to your theme:

    1. Open the?functions.php?in your child theme and add the code at the end.
    2. ?or install the?Code Snippets?plugin and apply this code as a snippet.

    After adding this code to your site, you still need to do one more thing. Change #search-results to the ID or class assigned to the element containing the search results. It’s best to do this by right-clicking any element within the search results and selecting Inspect (Chrome). This will open the developer tool, allowing you to locate your ID/class. Please refer to this screenshot for assistance: [link]. Unfortunately, I can’t assist you further as I don’t know the URL of your site or the name of your theme.

    // Scroll to the Search Results after the search results page loads
    add_action( 'wp_footer', function() { ?>
    	<script type="text/javascript">
    		jQuery(document).ready(function() {
    			let urlParams = new URLSearchParams(window.location.search);
    
    			if(urlParams.has('dgwt_wcas')) {
    				var searchResults = jQuery('#search-results'); // Here you have to replace the search results container with your ID or class (for .class use . instead #).
    
    				if(searchResults.length > 0) {
    					var scrollToPosition = searchResults.offset().top - 80;
    
    					// Scroll to search results 
    					jQuery('html, body').animate({
    						scrollTop: scrollToPosition
    					}, 1000); // Animation time
    				}
    			}
    		});
    	</script>
    <?php }, 9999 );

    Regards,
    Kris

    Thread Starter sm2dev

    (@kelg7)

    @c0nst This works great. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Scroll to results div’ is closed to new replies.