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 ) {
    if (is_search()) {
        // 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() {
    if (is_search()) {
	// 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>';
	}
    }
}

12 comments Relevanssi and Genesis

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

      1. 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

        1. 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.

          1. 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?

          2. 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.

  2. 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?

  3. Bought Relevanssi Permanent today (after reading the promise that PDF support is coming soon as part of the plugin!). I have all my WP sites using Genesis, I came across the same issue. The first part of the code worked fine in my theme’s functions.php. But the second part of the code just made all my pages blank.
    Looking for an answer, I found the following solution while scouting the Web. Very simple, and it works fine:
    https://www.clearbusiness.com/using-relevanssi-with-genesis-studio-wordpress-theme-framework/
    I have made the selection from “content” to “extract” for Archives in the Genesis settings for two websites, and instantly excerpts and highlighting have started working, without any need to change anything in code.
    Of course, is somebody really wants to have “content” instead of “extract” as the option for Archives, it won’t work. Otherwise, it is a very easy solution.

  4. And if one does not want to use “excerpts” instead of “content” (since this may have consequences for other things on a website), Sridhar Katakam has provided a piece that works fine for showing search results as excerpts when inserted in the theme’s functions.php :
    https://gist.github.com/srikat/6912382
    I have just applied it to a site using Modern Studio Pro theme, and it is working really fine!

Leave a Reply

Your email address will not be published. Required fields are marked *