Created
December 3, 2020 18:54
-
-
Save mgibbs189/8df52bf40e1a974408aba23be1a7c0a3 to your computer and use it in GitHub Desktop.
FacetWP - highlight template text containing search term
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
FWP.hooks.addAction('facetwp/loaded', function() { | |
var facet_name = 'search'; // EDIT ME | |
var allowed_elements = '.cat-item__title a, .cat-item__descr p'; // EDIT ME | |
var keywords = FWP.facets[facet_name].toString(); | |
if ('' !== keywords) { | |
$(allowed_elements).each(function() { | |
var results = $(this).html(); | |
results = results.replace(new RegExp(keywords, "gi"), (match) => `<mark>${match}</mark>`); | |
$(this).html(results); | |
}); | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment