#27246 closed enhancement (fixed)
Add function to get the excerpt outside the loop
Reported by: | Atoon | Owned by: | swissspidy |
---|---|---|---|
Milestone: | 4.5 | Priority: | normal |
Severity: | normal | Version: | 3.8.1 |
Component: | Posts, Post Types | Keywords: | has-patch has-unit-tests commit |
Focuses: | template | Cc: |
Description
There's currently a function called get_the_excerpt
, yet it don't work as the other similar ones as it don't support any arguments. I would like to have a core function that works like the_excerpt
, but outside the loop by specifying an ID.
Attachments (3)
Change History (22)
#2
@
10 years ago
- Keywords needs-patch added
- Milestone changed from Awaiting Review to Future Release
#4
in reply to:
↑ 3
@
10 years ago
Replying to husobj:
I notice get_the_excerpt() current accepts 1 parameter which is $deprecated since 2.3
It looks like it used to be
get_the_excerpt( $fakeit = true )
https://core.trac.wordpress.org/browser/tags/1.5/wp-includes/template-functions-post.php#L110 back as far as 1.5 but $fakeit it doesn't seem to be used anywhere so I presume we'd be OK to pass a post ID now like get_the_title()?
We'd still need to pass the $deprecated
argument, which definitely makes adding additional parameters a little clunky since you'd effectively call it as get_the_excerpt( '', $post_id );
#5
@
10 years ago
I was trying to get to the bottom of what the old $fakeit was.
It looks like it used to expect a boolean true/false value as to wether to 'fake' the excerpt content but can't confirm that this was ever implemented - see #4439. The parameter is there but not implemented as far back as WP 1.5 (I haven't tried to research back further)
If it could be confirmed that it used to expect a boolean, could we then check for a numeric value for that first parameter and then only handle that as a Post ID if that's the case?
It would be nice to bring it inline with other functions like get_the_title()
#7
in reply to:
↑ 6
@
10 years ago
Replying to SergeyBiryukov:
That still doesn't make it consistent with
get_the_content()
though...
True, hmm
@
9 years ago
Repurpose deprecated argument to optionally indicate a post other than, the current from "The Loop", to excerpt
#9
@
9 years ago
- Keywords has-patch added; needs-patch removed
Patch uploaded, but we still need unit tests.
#10
@
9 years ago
27246.2.diff is an updated patch that also adds some unit tests for get_the_excerpt()
.
#11
@
9 years ago
- Keywords has-unit-tests added; needs-unit-tests removed
- Milestone changed from Future Release to 4.5
#12
@
9 years ago
- Keywords commit added
I think it's safe to undeprecate it and treat it as a post ID if a numeric value or a post object is passed.
27246.2.diff implements this and I think is safe to add. The original param was deprecated a long time ago and I often ran into this "bug" before.
#13
@
9 years ago
- Owner set to swissspidy
- Status changed from new to accepted
27246.3.diff adds a new test for the deprecated notice.
#16
@
9 years ago
With similar ones I compare it with the_content
and that one also not have it. I rather would revert it since they are more like theme functions which are generally used in a loop.
Isn't it an option to put functions like this in WP_Post? and change the theme functions to call the methods in WP_Post. As a plugin developer I would love to use WP_Post more and doing something like this would be a good step.
#17
@
9 years ago
With similar template tags I meant get_permalink()
, get_the_title()
and basically any get_
function. They all allow passing a $post
while their the_
equivalents display the values for the current post. In theme templates, the_excerpt()
is usually used, whereas get_the_excerpt( $post )
can also be used by plugins — without manually needing to apply filters.
Isn't it an option to put functions like this in WP_Post? and change the theme functions to call the methods in WP_Post. As a plugin developer I would love to use WP_Post more and doing something like this would be a good step.
Oh, I think it is. I haven't found an existing ticket for that, but I've seen plenty of code examples of custom Post
decorator classes implementing something similar.
#18
follow-up:
↓ 19
@
9 years ago
That's a good point. I also made myself guilty with copy/pasting the code of get_the_excerpt()
/the_excerpt
. So it is a good thing to have. I'm just thinking how the future would look like. Would be great to have a roadmap for WP_Post and related classes.
#19
in reply to:
↑ 18
@
9 years ago
Replying to markoheijnen:
That's a good point. I also made myself guilty with copy/pasting the code of
get_the_excerpt()
/the_excerpt
. So it is a good thing to have. I'm just thinking how the future would look like. Would be great to have a roadmap for WP_Post and related classes.
For the record, I just found #12267 about this. It was recently closed as maybelater, but if there's interest in it.. Why not?
I notice get_the_excerpt() current accepts 1 parameter which is $deprecated since 2.3
It looks like it used to be
get_the_excerpt( $fakeit = true )
https://core.trac.wordpress.org/browser/tags/1.5/wp-includes/template-functions-post.php#L110 back as far as 1.5 but $fakeit it doesn't seem to be used anywhere so I presume we'd be OK to pass a post ID now like get_the_title()?I'm happy to write a patch for this.