Skip to content

Instantly share code, notes, and snippets.

@Narga
Last active December 15, 2015 05:49
Show Gist options
  • Save Narga/5211666 to your computer and use it in GitHub Desktop.
Save Narga/5211666 to your computer and use it in GitHub Desktop.
# Function to trim the excerpt
# 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
if(is_front_page() || is_archive() || is_search()) :
global $post;
$content = $post->post_excerpt;
$content = strip_shortcodes($content);
$content = str_replace(']]>', ']]>', $content);
$content = strip_tags($content);
# If an excerpt is set in the Optional Excerpt box
if($content) :
$content = apply_filters('the_excerpt', $content);
# If no excerpt is set
else :
$content = $post->post_content;
$excerpt_length = 50;
$words = explode(' ', $content, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '...<p><a class="more-link" href="' . get_permalink() . '" title="' . the_title_attribute('echo=0') . '"> ' . __( 'Read more &#187;', 'narga' ) . ' </a></p>');
$content = implode(' ', $words);
endif;
$content = '<p>' . $content . '</p>';
endif;
endif;
# Make sure to return the content
return $content;
}
# Replace content with excerpt
if (get_theme_mod('posts_excerpt') == 'enable') {
add_filter('the_content', 'narga_excerpts');
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment