WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 11 months ago

#36934 closed defect (bug)

Use of get_the_excerpt($post) is broken if post has no excerpt and you are inside a loop — at Initial Version

Reported by: magicroundabout Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.5
Component: Posts, Post Types Keywords: has-patch needs-testing has-unit-tests dev-feedback
Focuses: template Cc:

Description

In changeset @36319 (and @36321), a $post parameter was added to get_the_excerpt() in post-template.php. I was really excited when I discovered this but:

a) It's behaviour is inconsistent
b) It doesn't quite do what I'd hoped
c) It's broken in some circumstances

There are a few things wrong:

1) The behaviour is inconsistent depending on usage.

If you call get_the_excerpt() within the loop and if the post has no excerpt, then the post content will be stripped and truncated.

If you call it with the $post parameter and the post has no excerpt then nothing is returned.

Well...sometimes...

2) If you are inside a loop - say you're using get_the_excerpt($post) in a shortcode in a post - then you will actually get back the truncated content of the post from the loop that you are in.

This is because wp_trim_excerpt( $text ) does get_the_content('') if $text is empty.

I think that the fix for this is to add an optional $post parameter to wp_trim_excerpt() and process it accordingly.

We'll also need to update default-filters.php to pass the extra parameter on the get_the_excerpt filter hook.

This change has the added benefit that get_the_excerpt($post) works consistently, fetching and trimming the post content if the post has no defined excerpt. Hooray!! (Been wanting this for YEARS!)

I'm working on a patch. It would be good to write a test for this too but I have no idea how. Happy to take feedback.

Change History (1)

@magicroundabout
4 years ago

Attempt at patch

Note: See TracTickets for help on using tickets.