[Resolved] Decide which text is used as excerpt

Home Forums Support Decide which text is used as excerpt

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #1304537
    Michael

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

    #1304978
    David
    Staff
    Customer Support

    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/

    #1305196
    Michael

    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?

    #1305197
    Michael

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

    #1305221
    David
    Staff
    Customer Support

    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.

    #1305324
    Michael

    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

    #1305603
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #1306146
    Michael

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

    #1306641
    Tom
    Lead Developer
    Lead Developer
    #1379104
    Michael

    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

    #1379183
    David
    Staff
    Customer Support

    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' => ''
    );
    #1379187
    Michael

    Thank you David!

    #1379189
    David
    Staff
    Customer Support
Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.