Support » Plugin: Relevanssi - A Better Search » Not Highlighting on Page

  • I am noticing that if I search for a term that is found on a “normal” page whose content appears in WordPress’ default editor, the term is highlighted on the page and it automatically scrolls to the term. On pages that use a custom template where we use custom fields, it won’t highlight and scroll to the term.

    Here’s the website: http://www.waterwayplastics.com

    Working right: search for 810-1165

    Not working right: search for “Internal chamber”

    Any ideas?

    https://wordpress.org/plugins/relevanssi/

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

    (@msaari)

    That scrolling is not a Relevanssi feature, so I can’t help you with that.

    If the highlighting is powered by Relevanssi, that’ll require some changes in your custom template. Relevanssi by default only highlights terms in post content (because it’s done using the_content filter hook).

    Relevanssi has a function called relevanssi_highlight_terms(), which you can use to highlight the custom field content in your template.

    Thread Starter jjdualan

    (@jjdualan)

    I tried adding this to the page’s template but there’s no difference in what’s outputted.

    if (isset($wp_query->query_vars['s'])) {
       echo relevanssi_highlight_terms(get_field('additional_information'),$wp_query->query_vars['s']);
    }

    Here’s a sample URL http://waterwayplastics.com/products/pool-products/chlorinators/chlorinators-2/#s=”400-5200″

    Any ideas what I’m doing wrong?

    Plugin Author Mikko Saari

    (@msaari)

    Can you show me the complete template?

    Thread Starter jjdualan

    (@jjdualan)

    Here’s the template code:

    <?php
    /*
    Template Name: Product Detail
    */
    ?>
    <?php get_header(); ?>
    <div id="content_wrapper" class="clearfix secondary_sidebar">
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<div id="category_banner" class="grid_12 clearfix">
    		<div id="category_info">
    			<div>
    				<?php $title = get_field('product_title') ? get_field('product_title') : get_the_title(); echo('<div id="page_title">' . $title . '</div>'); ?>
    				<?php get_field('description') ? the_field('description') : ''; ?>
    				<?php if($post->post_parent) {
    					$parent_link = get_permalink($post->post_parent);
    					$parent_title = get_the_title($post->post_parent);
    					if ($parent_title <> 'Products') {
    				?>
    					<p><a href="<?php echo $parent_link; ?>">Return to <?php echo $parent_title; ?></a></p>
    				<?php } } ?>
    			</div>
    		</div>
    		<?php $image = wp_get_attachment_image_src(get_field('product_image'), 'category-thumb'); ?>
    		<img src="<?php echo $image[0]; ?>" alt="" style="display: block; margin: 0 auto;" />
    	</div>
    	<!-- Begin Additional Info Toggle Box -->
    	<?php if (get_field('additional_information')) { ?>
    	<div class="grid_12">
    		<div id="product_specs_box_wrapper">
    			<a href="#" id="togglebox">
    				<h2><?php $boxheading = get_field('product_toggle_box_heading') ? get_field('product_toggle_box_heading') : "Specifications"; echo $boxheading; ?></h2>
    			</a>
    			<div id="product_specs_box" class="clearfix">
    				<?php 
    
                        if (isset($wp_query->query_vars['s'])) {
                            echo relevanssi_highlight_terms(get_field('additional_information'),$wp_query->query_vars['s']);
                        } else {
                            the_field('additional_information');
                        }
                    ?>
    			</div>
    		</div>
    	</div>
    	<script type="text/javascript">
    	jQuery(document).ready(function($) {
    		$('#togglebox').click(function() {
    			toggleLink = $(this).parent().find('#togglebox');
    			if (toggleLink.hasClass('expanded')) {
    				toggleLink.removeClass('expanded');
    			} else {
    				toggleLink.addClass('expanded');
    			}
    			$('#product_specs_box').fadeToggle();
    			return false;
    		});
    		$('.product_chart tr:nth-child(even)').addClass('alt');
    
    		console.log(document.referrer);
    	});
    	</script>
    	<?php } ?>
    	<!-- End Additional Info Toggle Box -->
      <?php endwhile; else: ?>
      <p>
        <?php _e('Sorry, no posts matched your criteria.'); ?>
      </p>
      <?php endif; ?>
    </div>
    <?php get_footer(); ?>
    Plugin Author Mikko Saari

    (@msaari)

    Instead of $wp_query->query_vars['s']), use get_search_query() or add global $wp_query; before it.

    Thread Starter jjdualan

    (@jjdualan)

    I tried it both ways:

    global $wp_query;
                        echo relevanssi_highlight_terms(get_field('additional_information'),$wp_query->query_vars['s']);

    and

    global $wp_query;
                        echo relevanssi_highlight_terms(get_field('additional_information'),get_search_query());

    But still no change. I tried hard coding a term in the code and the highlighting worked, so it looks like it’s just not able to access the variable in the querystring.

    Plugin Author Mikko Saari

    (@msaari)

    Ah, indeed. It is not a search results template, so the search query is going to be empty. You need to get the search query from $_SERVER[‘HTTP_REFERER’].

    Try this:

    echo relevanssi_highlight_in_docs(get_field('additional_information'));

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Not Highlighting on Page’ is closed to new replies.