Last active
January 24, 2017 07:54
-
-
Save gregrickaby/9312ccf4f90ed7b8e528 to your computer and use it in GitHub Desktop.
Customize the excerpt length, strip tags, append, and change the "Read More" text
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 | |
// DO NOT INCLUDE OPENING PHP TAG | |
/** | |
* Customize the excerpt length and strip tags | |
*/ | |
function custom_excerpt ( $text ) { | |
global $post; | |
// Set defaults | |
$excerpt_length = 30; // excerpt length in words | |
$allowed_html_tags = ''; // allowed html tags, e.g., <a><p><strong> etc... | |
$append_with = '...'; // append the excerpt with something special, e.g., ... or [...] | |
// If there is an excerpt | |
if ( '' === $text ) { | |
$text = get_the_content( '' ); | |
$text = str_replace( '\]\]\>', ']]>', $text ); | |
$text = strip_shortcodes( $text ); | |
$text = apply_filters( 'the_content', $text ); | |
$text = strip_tags( $text, $allowed_html_tags ); | |
$words = explode( ' ', $text, $excerpt_length + 1 ); | |
// Shorten the excerpt | |
if ( count( $words ) > $excerpt_length ) { | |
array_pop( $words ); | |
array_push( $words, $append_with ); | |
$text = implode( ' ', $words ); | |
} | |
} | |
return $text; | |
} | |
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' ); | |
add_filter( 'get_the_excerpt', 'custom_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 | |
// DO NOT INCLUDE OPENING PHP TAG | |
/** | |
* Customize the Read More text | |
*/ | |
function custom_excerpt_read_more( $output ) { | |
return $output . '<a class="read-more" href="'. get_permalink( get_the_ID() ) . '"> ' . __( 'Read More', 'textdomain' ) . '</a>'; | |
} | |
add_filter( 'the_excerpt', 'custom_excerpt_read_more' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment