Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Trimming Manual Excerpt

  • Two questions:

    1. Is the behavior of this code (https://docs.generatepress.com/article/activating-read-custom-excerpt/) supposed to actually trim the excerpt, or just add a read more link to the end of the custom excerpt regardless of length?

    2. Is it possible to make this code (or some other code) trim the excerpt of a custom post type (podcast) generated by Podlove? As you can see on this page: https://frugalflyer.ca/blog/, the excerpts for my podcast episodes are long and don’t get trimmed.

    Thanks,
    Reed

  • Hi there,

    1. The snippet only adds a Read More button to posts with custom excerpts, as by default it won’t appear if the post uses a custom excerpt.

    2. Assuming Podlove uses custom excerpt, this snippet should work:

    
    add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
        if ( has_excerpt( $post ) && ! is_single() ) {
            $excerpt_count  = 20;
            $excerpt_length = apply_filters( 'excerpt_length', $excerpt_count );
            $excerpt_more   = apply_filters( 'excerpt_more', ' ' . '→' );
            $excerpt        = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
        }
        return $excerpt;
    }, 10, 2 );
    

    Just set the $excerpt_count to the number of words you want to display.

  • This sort of addresses my point #1 as it only trims when the excerpt exceeds what is set in the customizer. However it does’t appear to follow the $excerpt_count that I set, and also isn’t applying to podcast post type.

  • Hi there,

    the snippet will apply only to a custom Excerpt.
    And it sets its lenght using the excerpt_length filter hook, which although that snippet is setting it to 20. There are other functions such as the Customizer setting that also use that same filter and pass it a different value, hence you’re custom excerpts are being trimmed to the customizer length.

    If you wanted to trim all custom excerpts to an exact length and ignore other filter options you would use:

    
    add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
        if ( has_excerpt( $post ) && ! is_single() ) {
            $excerpt_length  = 20;
            $excerpt_more   = apply_filters( 'excerpt_more', ' ' . '→' );
            $excerpt        = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
        }
        return $excerpt;
    }, 10, 2 );
    

    Now, the Podlove post type, how is the excerpt set- Is it using the default Excerpt field in the editor ?
    Or is it using its own custom field ?

  • Nope unfortunately it is using a field called summary.

    Ideally if possible, I’d like to trim all episode excerpts to a custom length and leave everything else (ie not affect posts, pages, etc).

  • Nope unfortunately it is using a field called summary.

    Is it a post meta? But it does not look like a post meta, it looks like the custom excerpt based on the front end result.

    Can you confirm? Or can you take a screenshot of the field where you enter the excerpt?

  • Yes here is what it looks like: https://prnt.sc/L1-1-eoeYr00

    If it helps, here is their github: https://github.com/podlove/podlove-publisher

    Also found a few items from their readme:

    = 1.2.16-alpha =
    * Enhancement: remove episode excerpt support in favor of episode summary

    = 1.2.13-alpha =
    * Enhancement: use episode summary as excerpt if available

  • Then can you NOT use summary but use the default excerpt instead?

    As if it’s the plugin’s setting, you need to reach out the plugin’s support and let them provide the code to trim the post meta.

  • The summary is important data that is passed to RSS feed to be published to podcast directories like Spotify. Also the default excerpt field is not available.

  • What is interesting is on this page (https://frugalflyer.ca/podcast/), built with query loop, the episode summary is correctly read in as excerpt and wp block settings can trim it just fine.

    So it is being treated as a custom excerpt? But the code provided above and here (https://docs.generatepress.com/article/activating-read-custom-excerpt/) aren’t able to trim it.

    Anyways yes I will see if Podlove support can help. Thanks.

  • What if you use this code and set the word count in Customizer?
    https://generate.support/topic/trimming-manual-excerpt/#post-114442

  • Truncates all custom excerpts but not the episodes.

  • Reading your previous reply here https://generate.support/topic/trimming-manual-excerpt/#post-114423

    So the word count does not match the 20 you set in the code, or the word count you set in the customizer?

    How many words does it show with that code?

  • Ya that was a separate issue. I set the number in that code to 5, and it was still showing 25 which is what the customizer is set to. But so far no piece of code has ever had any effect on episodes.

    I actually already had a ticket open with Podlove, I just bumped it. Unfortunately, they just tend to be a little less helpful and responsive than you guys which is why i’d asked here :). Maybe they can even just point me in the right direction to figure out how to pinpoint the excerpt that podlove is generating.

  • I downloaded the plugin and played with it, try this code, I removed the condition that for the posts to have custom excerpt as the plugin has removed it, and changed the priority of the code. Let me know if this one works!

    add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
        if ( ! is_single() ) {
            $excerpt_length  = 20;
            $excerpt_more   = apply_filters( 'excerpt_more', ' ' . '→' );
            $excerpt        = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
        }
        return $excerpt;
    }, 99, 2 );
  • Beautiful it works ! 🙂

    If I wanted this to only apply to the episodes and not regular posts or pages, is there an easy way to do that?

    If not, I can manage.

Viewing 16 posts - 1 through 16 (of 19 total)
  • You must be logged in to reply to this topic.