[wp-hackers] Post excerpts within the_content filter callback functions
Dougal Campbell
dougal at gunters.org
Wed Jun 16 16:20:16 UTC 2010
On Jun 15 2010 10:55 PM, John Blackbourn wrote:
> [...]
> The reason this doesn't work is because this will cause an infinite
> loop in PHP if the post's post_excerpt field is empty. Why? When
> get_the_excerpt() is called, if the post's post_excerpt field is empty
> then wp_trim_excerpt() will generate an excerpt using the post
> content, and this post content has the_content filter applied to it,
> giving us our infinite loop.
>
Maybe try something like...
function my_content_callback( $content ) {
global $post;
static $flag = 0;
while (0 == $flag++) {
$excerpt = get_the_excerpt( $post->ID );
// do something with $excerpt
}
return $content;
}
add_filter( 'the_content', 'my_content_callback' );
The first time through, it should call your excerpt function, but when
wp_trim_excerpt() calls your filter again, the static $flag variable
should be 1, and skip calling get_the_excerpt() again.
I haven't actually tested it, but I think that should work.
--
Dougal Campbell <dougal at gunters.org>
http://dougal.gunters.org/
http://twitter.com/dougal
http://twitual.com/
More information about the wp-hackers
mailing list