Skip to main contentSkip to footer

Relevanssi Premium 2.11 introduces a new feature: multi-part excerpts. There’s a new setting on the Excerpts & Highlights settings tab, “Number of excerpt snippets”, and that lets you define the maximum number of excerpt snippets Relevanssi generates for each post.

How does it work

This option is only available when the excerpt length is defined in words. If the excerpt length is defined in characters, the option still appears, but has no effect: there’ll be just one excerpt snippet for each post.

This is the maximum number of excerpt snippets. If Relevanssi can’t find enough good excerpts, for example if the search term appear only once in the document, there’ll be fewer excerpts. Relevanssi also tries to avoid overlapping excerpts and will eliminate excerpts that look too much like excerpts it has already chosen.

The different parts will be combined together in the order of relevance: the best part comes first, then the second-best and so on. Relevanssi won’t order them in the order the parts appear in the post, and can’t show any indication of where in the post the part appears.

Styling the excerpts

Each part is wrapped in a span tab with the class excerpt_part. In order to make the excerpt parts stand out from each other, you need to CSS styles. This would be a fairly basic way to do it:

span.excerpt_part {
    display: block;
    border: thin solid #aaa;
    margin: 10px;
    padding: 20px;
}

If your excerpts include other sources (manually entered post excerpts, comments), Relevanssi knows that and you can display that information on the search results page:

Relevanssi excerpts with excerpt source displayed

Achieving this is a two-part process. First, add this to your site to modify the excerpt part with the relevanssi_excerpt_part filter to show the source data:

add_filter( 'relevanssi_excerpt_part', 'rlv_excerpt_parts', 10, 2 );
function rlv_excerpt_parts( $excerpt_text, $excerpt ) {
    return '<span class="excerpt_part"><span class="excerpt_source">'
        . $excerpt['source']
        . '</span>'
        . $excerpt['text']
        . '</span>';
}

Then add some styles to make it look nicer:

.excerpt_part .excerpt_source {
	display: inline-block;
	text-transform: uppercase;
	font-size: 50%;
	color: #555;
	margin-right: 20px;
	padding: 5px;
	border: thin solid #999;
}

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Related searches feature
John Blackbourn wrote a cool related searches feature for Relevanssi Premium. It’s included from version 1.7.4. In order to use…
Adjusting search throttle
If Relevanssi Premium causes out-of-memory problems, make sure you have the “Throttle searches” option checked. However, if that doesn’t work…
Unyson

…cover most of the indexed content. It gets text elements added in the posts and text elements inside other elements (like columns). The title element gets headings, text gets all the text box contents. Of course, it’s also possible to modify this function to capture more content or deeper hierarchies….…custom field should only * appear once, so we just take $value[0]. */ $content = json_decode( $value[0] ); $string = ”; foreach ( $content as $section ) { if ( isset( $section->atts->title ) ) { $string .= $section->atts->title; } if ( isset( $section->atts->text ) ) { $string .= $section->atts->text; }……if ( isset( $section->_items ) ) { foreach ( $section->_items as $item ) { if ( isset( $item->_items ) ) { foreach ( $item->_items as $child_item ) { if ( isset( $child_item->atts->title ) ) { $string .= $child_item->atts->title; } if ( isset( $child_item->atts->text ) ) { $string .= $child_item->atts->text; }…

Related Posts:

Comment Section:

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed