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">→</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 »', '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.
$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.