• Resolved bkalish

    (@bkalish)


    I like the custom search result snippets, but wish I could retain the context provided by the default excerpts, particularly when the user has provided their own custom excerpt. Is there a way I can combine the two? Ideally I would want the following:

    1. If the user has provided a custom excerpt, use it at the beginning of the excerpt.

    2. If the the user has not provided a custom excerpt and Relevanssi’s search result snippet does not start at the beginning of the post, then include the default WordPress excerpt at the beginning of the excerpt.

    3. In all cases end with Relevannsi’s search result snippet, so that we get the benefit of the highlighted search terms.

    Given all the hooks Relevannsi has provided it seems like this should be possible, but I haven’t found the hook I need. (Or perhaps I need to do something clever with filter priority?)

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

    (@msaari)

    Sounds feasible to me. You need the relevanssi_excerpt_part. That filters will give you the Relevanssi-generated excerpt, and will also tell you whether the excerpt is from the beginning of the post.

    Then just create a function that does your 1-2-3 logic and adds the necessary stuff to the excerpt. The filter hook gets the post ID as a parameter, so you can fetch the original post and get the excerpt and post content there.

    Thread Starter bkalish

    (@bkalish)

    Wonderful! I hadn’t found that filter yet and I agree it looks like it has what I need. I’ll give it a try.

    Thread Starter bkalish

    (@bkalish)

    One note from my work so far: relevanssi_excerpt_part does not in fact get called with the post ID, and defining a filter that expects a third argument results in an error. That said, get_the_ID() and similar functions seem to be working fine, so that may not be necessary.

    Thread Starter bkalish

    (@bkalish)

    I now have something very much along the lines of what I wanted. One more question: is there an easy way to apply Relevanssi’s highlighting to my customized excerpt?

    Thread Starter bkalish

    (@bkalish)

    Ah, I just found relevanssi_highlight_terms. I’m all set. Thanks for making Relevanssi so comprehensive and customizable! I work at a small non-profit, and I really appreciate having this in the basic version of the plugin.

    Plugin Author Mikko Saari

    (@msaari)

    relevanssi_excerpt_part does get the post ID:

    $excerpt_text .= apply_filters(
    	'relevanssi_excerpt_part',
    	'<span class="excerpt_part">' . $excerpt['text'] . '</span>',
    	$excerpt,
    	$post->ID
    );

    The first parameter is the excerpt part string, the second parameter is the full excerpt object and the third one is the post ID. relevanssi_excerpt only gets two parameters, the excerpt string and the post ID.

    Good to hear you got this sorted out, though.

    Thread Starter bkalish

    (@bkalish)

    Strange. When I define my filter with the signature
    function my_relevanssi_excerpt_part( string $excerpt_text, array $excerpt) : string
    it works fine but when I use
    function my_relevanssi_excerpt_part( string $excerpt_text, array $excerpt, int $post_id ) : string
    I get a fatal error:

    Fatal error: Uncaught ArgumentCountError: Too few arguments to function my_relevanssi_excerpt_part(), 2 passed in ... and exactly 3 expected ...

    • This reply was modified 3 years, 4 months ago by bkalish.
    Plugin Author Mikko Saari

    (@msaari)

    And the add_filter() has the correct number of parameters? Or has it just 2, not 3?

    Thread Starter bkalish

    (@bkalish)

    Ah! That was the problem!

    I modeled my “add filter” after the illustration in the documentation (https://www.relevanssi.com/user-manual/filter-hooks/relevanssi_excerpt_part/), but of course that example only used the first two parameters. Thanks for catching this.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom Excerpts’ is closed to new replies.