0

I'm planning to display the post excerpt instead of the content if available and auto generate excerpt from the content if not available with the function below:

<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'blabla' ) ); ?>

in functions.php I'm adding:

# Function to trim the excerpt
if (!function_exists('narga_excerpts')) :
    function narga_excerpts($content = false) {
        # If is the home page, an archive, or search results
            global $post;
            $excerpt_length = 40;
        # If an excerpt is set in the Optional Excerpt box
        if ( $post->post_excerpt ) {
        $content = the_excerpt();
        function custom_excerpt_length( $length ) {
            return 20;
        }
        add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
        function excerpt_readmore($more) {
        return '... <a title="' . the_title_attribute('echo=0') . '" href="'. get_permalink($post->ID) . '" class="more-link">  ' . __( 'Read more &#187;', 'narga' ) . ' </a>';
        }
            add_filter('excerpt_more', 'excerpt_readmore');
        }
        # If no excerpt is set
        else {
            $content = $post->post_content;
            $words = explode(' ', $content, $excerpt_length + 1);
            if(count($words) > $excerpt_length) {
                array_pop($words);
                $content = implode(' ', $words);
            }
        }
        $content = '<p>' . $content . '</p>';
        # Make sure to return the content
        return $content;
    }
# Replace content with excerpt
add_filter('the_content', 'narga_excerpts');
endif;

But it does not work.

  • 3
    "But it's not works" is useless. Tell us what it does that you are not expecting and/or what doesn't do that you are expecting it to do. – Charles Clarkson May 16 '13 at 9:49
  • Sorry about little information, at the line 9 $content = the_excerpt(); the problem is this line, the function will check if the excerpt exist then the valua of $content is the post excerpt that I can filte like default excerpt. But I can't find the correct solution to do it. Current function will drive WordPress to infinite loop then the browser get the reset error only. – Narga May 16 '13 at 13:48
2

Change line 9 to:

$content = get_the_excerpt();

the_excerpt() echoes the excerpt. get_the_excerpt() returns it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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