Short and sweet — I wanted to deliver up the excerpt of a post in the place of the_content
on certain pages. Naturally, if no excerpt is present one would be generated from the content. I understand I can just go in and edit the theme files, but I'd like a less-invasive method.
Seeing how something like the following works:
add_action( 'the_content', 'myFunc' );
function myFunc ( $content ) {
the_title();
}
I thought I would try this
add_action( 'the_content', 'myFunc' );
function myFunc ( $content ) {
the_excerpt();
}
This. Fails. Gloriously. It results in a fatal error, actually:
FATAL ERROR: MAXIMUM FUNCTION NESTING LEVEL OF '100' REACHED, ABORTING! IN C:\WAMP\WWW\WORDPRESS\WP-INCLUDES\POST.PHP ON LINE 555
This is followed by a massive call stack, which appears to have been set into a mad frenzy of recursive calls.
Turning the Tables
So this got me curious, and I tried the opposite. Deliver the_content
in the place of the_excerpt
:
add_action( 'the_excerpt', 'myFunc' );
function myFunc ( $content ) {
the_content();
}
This, to my surprise, worked just fine. So what I'm gathering is that the_excerpt
calls the_content
, but the_content
doesn't call the_excerpt
- this would certainly explain the fact that it works one way, but the other seems to be set off in a massive fit of recursion.
Not being very familiar with the inner-workings of these two function, I was curious what the community here could provide in way of insight. What's the problem with hooking into the_content
and calling the_excerpt
?
How can I achieve this effect if not by calling the_content
directly? I understand that I could provide my own custom excerpt logic, but this would de-normalize the excerpt logic in my site, meaning I'd have some excerpts being created via custom code in the functions.php, and others created via the internal methods of WordPress - ideally, I would avoid this and use only the internal methods if possible.