Created
February 23, 2012 20:37
-
-
Save FernE97/1894927 to your computer and use it in GitHub Desktop.
PHP: WordPress Dynamic Excerpt
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 | |
function the_excerpt_dynamic($num_words) { | |
global $post; | |
$text = $post->post_excerpt; | |
if ( '' == $text ) { | |
$text = get_the_content(''); | |
$text = apply_filters( 'the_content', $text ); | |
$text = str_replace( ']]>', ']]>', $text ); | |
} | |
$text = strip_shortcodes( $text ); // optional | |
$text = strip_tags( $text ); // optional, use ' $text = strip_tags($text,'<p><a>'); ' to keep some formats | |
// separate the words of the string into an array so we can parse it | |
$words = explode( ' ', $text ); | |
$total = count( $words ); | |
// get only the amount of words that we want to include in the excerpt | |
$words = array_slice( $words, 0, $num_words ); | |
$words[] = '… <a class="more-link" href="'. get_permalink($post->ID) .'">Read More</a>'; | |
// now put it back together | |
$text = implode( ' ', $words ); | |
echo apply_filters( 'the_excerpt', $text ); | |
} | |
// call in function | |
// if ( function_exists( 'the_excerpt_dynamic' ) ) the_excerpt_dynamic(40); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment