Created
May 22, 2013 22:32
-
-
Save FernE97/5631479 to your computer and use it in GitHub Desktop.
PHP: WordPress - Custom Excerpt Length Template Part
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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