0

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.

2 Answers 2

2

All good examples. But they weren't working for me with the theme (pinboard) and plugin I was using (secondary-html-content) and what I wanted to do.

The first challenge was to make sure that I get both pieces of content instead of just one. The second challenge was to replace the excerpt on the homepage with the actual content.

So I created a new plugin that does this:

function okmAddingContentExcerpt() {
    global $post;
    $content = '<div class="comment-first">'.$post->post_content.'</div>';
    // Adia Review is the name of the label from secondary-html-content plugin
    $content .= '<div class="comment-second">'.get_secondary_content('Adia Review',$post->ID).'</div>';
    return $content;
}
add_filter('the_excerpt', 'okmAddingContentExcerpt');

function okmAddingContentSingle() {
    global $post;
    $content = '<div class="comment-first">'.$post->post_content.'</div>';
    // remove the filter so that it doesn't loop over and over
    remove_filter('the_content','okmAddingContentSingle');
    $content .= '<div class="comment-second">'.get_secondary_content('Adia Review',$post->ID).'</div>';
    return $content;
}
add_filter('the_content', 'okmAddingContentSingle');

Notice how I wanted to keep the content instead of the excerpt on the main page.

Oh yeah, if you want to download the plugin, I added it to my blog-site: http://okmaya.com/wordpress-plugin-for-pinboard-theme-and-secondary-html-content-plugin/

1

Actually, the_excerpt() calls get_the_excerpt(), which outputs $post->post_excerpt.

One thing, also: shouldn't you be using add_filter() instead of add_action()?

Why are you using the_excerpt()/the_content()/the_title(), rather than setting $content = get_the_excerpt() etc., and then returning $content? e.g. this:

add_filter( 'the_content', 'myFunc' );
function myFunc ( $content ) {
  the_excerpt();
}

...would normally be written as such:

add_filter( 'the_content', 'myFunc' );
function myFunc ( $content ) {
  $content = get_the_excerpt();
  return $content;
}

Not sure if any of that will address your problem of the recursive loop...

EDIT

Your last comment gave me an idea: why not just pass $content through wp_trim_excerpt() and be done with it?

add_filter( 'the_content', 'myFunc' );
function myFunc( $content ) {
    $excerpt = wp_trim_excerpt( $content );
    return $excerpt;
}

EDIT 2

Okay, I'm with you now. How about we roll our own excerpt function?

add_filter( 'the_content', 'myFunc' );
function myFunc( $content ) {
    $text = strip_shortcodes( $content );
    $text = str_replace(']]>', ']]&gt;', $text);
    $text = strip_tags($text);
    $excerpt_length = apply_filters('excerpt_length', 55);
    $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    if ( count($words) > $excerpt_length ) {
         array_pop($words);
         $text = implode(' ', $words);
         $text = $text . $excerpt_more;
     } else {
         $text = implode(' ', $words);
     }        
     return $text;
}
7
  • I didn't mean the_excerpt calls the_content directly, sorry for the confusion. the_excerpt calls wp_trim_excerpt which itself calls get_the_content. I'm not sure where things are going crazy, to be honest. The other suggestions in your answer didn't help, unfortunately. Whether I use add_action or add_filter, it made no difference in this case. I also moved to returning the values as opposed to implicitly outputting them with the the_ functions - still no improvement.
    – Sampson
    Jul 12, 2011 at 19:26
  • I just realized I forgot one important bit of information. There are no excerpts - the excerpts ought to be generated from the content. So something like global $post; return $post->post_excerpt; would work, but would be empty.
    – Sampson
    Jul 12, 2011 at 19:45
  • Hmm... okay, see updated answer for a different idea. Jul 12, 2011 at 20:11
  • I've tried that as well, actually. Oddly enough it only returns a formatted content - no limit in chars as expected.
    – Sampson
    Jul 12, 2011 at 20:17
  • wp_trim_excerpt() calls $text = apply_filters('the_content', $text); which is the cause of the recursive calls. maybe try to extract the code from wp_trim_excerpt() from /wp-includes/formatting.php line 1830, leaving the above line away - although i have no idea how essential this line is for the excerpts.
    – Michael
    Jul 12, 2011 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.