relevanssi_the_title( $before = true, string $after = '', bool $echo = true )
Prints out the post title with Relevanssi highlighting applied.
From version 2.12.3 (Premium) / 4.10.3 (free) onwards replaces the_title(), supporting the same arguments. Before that, the signature looks like this:
relevanssi_the_title( $echo = true )
Source: /lib/utils.php (/lib/common.php before 2.12.3 / 4.10.3)
Parameters
$before
(string) (optional) A string that is prepended to the title.
Default: null
$after
(string) (optional) A string that is appended to the title.
Default: an empty string
$echo
(boolean) (optional) If true, echo out the result. If false, returns the result.
Default: true
Returns
If $echo
is false, returns the tag list as a string.
Usage
The functions works like the_title()
, but uses the title with Relevanssi highlights included from $post->post_highlighted_title
, if one is available (if not, the function falls back to $post->post_title
).
The post is taken from the global $post
object, so this function can only be used within The Loop. For a version that can be used outside the Loop, you can use relevanssi_get_the_title()
.
Using this function is required for getting the highlighted results. Relevanssi used to add the highlights to titles from the_title()
, but that resulted in highlighting appearing in wrong places, like menus or widgets, so that’s the reason for this extra complication.
If you don’t have a problem with menus, here’s a method of using the_title()
to show highlighted titles. Add this to your site:
add_filter( 'the_title', function( $title, $id ) { $post = relevanssi_get_post( $id ); if ( ! empty ( $post->post_highlighted_title ) ) { $title = $post->post_highlighted_title; } return $title; }, 10, 2 );