Site logo

Archived topic

Decide which text is used as excerpt

12 replies · Started by Michael on May 28, 2020

Viewing posts 1–13 of 13

I hope all is well.

I have a follow-up question on the category page:
Currently I have set the Excerpt word count to 56, which on my site http://www.blojfri.se/pottrana is just fine.

But for my new site http://www.sauberwerden.de/sauberwerden, I have a different set-up on the pages in that specific category. I would still like the the excerpt word count to be 56. But I would prefer if the excerpt did not start from the top of the relevant page.

If you go to one of the pages in the category you will see that I always start the page with the following text: Hilfeartikel für diejenigen, die Sauber! Hand in Hand weg von der Windel befolgen.

This means that that text is also always first in the excerpt.

Is there a way to make the excerpt start a bit in on the text?

I’m thinking either
– “hard-coding” excerpt (but that sounds difficult and tedious) or
– Somehow “hiding” the first sentence or two on page (so that the excerpt function does not see it).

Hi there.

try adding this PHP Snippet:

function replace_content_on_the_fly($text){
    $replace = array(
        // 'words to find' => 'replace with this'
        'This is the text to be replaced with' => ''
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
    }
add_filter('the_excerpt', 'replace_content_on_the_fly');

https://docs.generatepress.com/article/adding-php/

Hi David,

Nice to chat again :)

I assume adding that snippet means I would need to manually write the excerpt for each specific page in the category? And that I would need one snippet per page?

Isn't there an "easier" way?

I'm thinking about when I for instance receive some emails from marketers. It's quite often that the one-liner preview, next to the subject, in the inbox, is then not visible anywhere once I open the email the email. I believe it is called "Hidden preview text" ( see a bit down on this page: https://www.litmus.com/blog/the-ultimate-guide-to-preview-text-support/ ).

They have this solution:

Insert preview text here.

So I guess I could potentially copy a part of the text I would like to be shown in the excerpt, and place that inside the div at the top of the page?

The div did not show... but it is visible on the link if you scroll down to that header

That code i provided only needs to be added once to your site, as per this documents instructions:

https://docs.generatepress.com/article/adding-php/

Within the code you need to update this line to include the text you want to remove:

'This is the text to be replaced with' => ''

Then whenever an excerpt contains that text it will automatically remove it.

Ok!
I added the code, and it works :)

However, and pardon my stupid question, but how is the code limited to only searching for that specific text in the excerpt part, on that particular category page?

It feels like I am missing to include in the code to only perform this replace function on a specific category page? Like I can do with an array, when I want to block certain pages from the search function.

Regards
Michael

Hi there,

Try this:

add_filter('the_excerpt', function( $text ) {
    if ( is_category( 'your-category-slug' ) ) {
        $replace = array(
            // 'words to find' => 'replace with this'
            'This is the text to be replaced with' => ''
        );

        $text = str_replace(array_keys($replace), $replace, $text);
    }

    return $text;
} );

Just update the slug and you should be good to go :)

Thank Tom (and David)! You guys are simply awesome :)

Glad we could help :)

Hi again,
I have a follow-up question on the snippet I installed previously.

How do I add more words that are to be filtered out (replaced with blank)?

I've tried several different ways of changing the snippet, but could not make it works.

Best regards

Michael

You can add multiple strings in the array eg.:

$replace = array(
   // 'words to find' => 'replace with this'
   'This is the text to be replaced with' => '',
   'This is the other text to be replaced with' => ''
);

Thank you David!

You're welcome

This archived topic is closed to new replies.