Posted on

Relevanssi and Genesis

In some cases Relevanssi requires modifications to search result template for the search results to work properly (in particular when using Relevanssi Premium to search user profiles and taxonomy terms). Adjusting the templates is really simple, usually just changing the_permalink() to echo relevanssi_get_permalink() is enough, but framework themes like Genesis make small changes like these more complicated.

Sarah Moyer took the effort to figure out how to make the necessary changes in Genesis. First, in order to get the correct permalink in the post title, add this code to your functions.php:

/* Modify post titles to add Relevanssi permalink */
// help from http://adamcap.com/code/filter-genesis-h1-post-titles-to-add-for-styling/
add_filter( 'genesis_post_title_output', 'child_post_title_output', 15 );
function child_post_title_output( $title ) {
    // change h2 to h1 depending if you have a search page title or not
    if ( function_exists( 'relevanssi_get_permalink' ) ) {
        $title = sprintf( '<h2 class="entry-title" itemprop="headline"><a href="%s" rel="bookmark">%s</a></h2>', esc_url( relevanssi_get_permalink() ), apply_filters( 'genesis_post_title_text', get_the_title() ) );
    }
    return $title;
}

Then, for excerpts:

// Modify content to add Featured Images and Relevanssi permalink
remove_all_actions( 'genesis_entry_content' );
add_action( 'genesis_entry_content', 'child_do_post_excerpt' );
function child_do_post_excerpt() {
	// Get featured image if page or post
	the_post_thumbnail( 'medium', array( 'class' => 'alignright' ) );
 
	// Get featured image if WooCommerce product category
	global $post;
	$thumbnail_id = get_woocommerce_term_meta( $post->term_id, 'thumbnail_id', true );
	echo wp_get_attachment_image( $thumbnail_id, 'medium', '', array( 'class' => 'alignright' ) );
 
	// Get excerpt rather than the_content() so Relevanssi can grab a snippet and highlight search terms
	the_excerpt();
 
	// Get 'Read More' link with Relevanssi permalink
	if ( function_exists( 'relevanssi_get_permalink' ) ) {
		echo '<a class="read-more-link" href="' . esc_url( relevanssi_get_permalink() ) . '">View more »</a>';
	}
}
  • Eric

    How can I use these scripts to add featured image in my search results?Can a free version do?Looking for an answer……

    • Put the second code snippet to your theme functions.php, that should get you featured images in search results.

      • Eric

        Nooooo!I did what what you told me to and my website now is completely white and blank!!!!Can you tell me how to find “theme functions.php” in my FTP?So I can save my website!Thanks

        • The functions.php file is in your theme folder, which is probably in /wp-content/themes/yourtheme/functions.php.

          Using WordPress code editors is not a good idea unless your familiar with PHP code, as even a single typo or pasting the code to the wrong place will bring your site down like this.

          • Eric

            Thanks a lot!I just bring my website back!So Is there a safer way to let the Featured Images appear in the search results?

          • That’s pretty much the only way. To make it safer, have a PHP developer add the code to your site. That should take an experienced developer about five minutes.

          • Eric

            Thanks,I will be more careful next time!

  • Rob P

    I’m running WP 4.4.1, Genesis 2.2.6 and Child theme “magazine”. I’ve tried this code and also the instructions regarding using genesis “post except”. Can’t get Relevanssi to work at all, I only see the default search results. Any more thoughts?

    • Sorry, no idea. I don’t support Genesis, it’s just too difficult to figure out how it’s supposed to work.