Skip to content

Instantly share code, notes, and snippets.

@FernE97
Created May 22, 2013 22:32
Show Gist options
  • Save FernE97/5631479 to your computer and use it in GitHub Desktop.
Save FernE97/5631479 to your computer and use it in GitHub Desktop.
PHP: WordPress - Custom Excerpt Length Template Part
<?php
/**
* Custom Excerpt
*
* Needs $word_count set from the template it's being called from
*
* Template Usage:
*
* $word_count = 60;
* get_template_part( 'parts/excerpt' );
*
*/
global $word_count;
// sets a default word count fallback
if ( empty( $word_count ) ) $word_count = 36;
$text = get_the_content();
$excerpt = get_the_excerpt();
$custom_excerpt = custom_trim_words( $text, $num_words = $word_count, $more = '... ' );
// Echo custom excerpt if the excerpt is empty
if ( ! empty( $post->post_excerpt ) ) {
echo $excerpt;
} else {
echo $custom_excerpt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment