Skip to content

Instantly share code, notes, and snippets.

@tankbar
Forked from gregrickaby/functions.php
Created January 24, 2017 07:54
Show Gist options
  • Save tankbar/50db59ce0556442e7325a04480a4d9f1 to your computer and use it in GitHub Desktop.
Save tankbar/50db59ce0556442e7325a04480a4d9f1 to your computer and use it in GitHub Desktop.
Customize the excerpt length, strip tags, append, and change the "Read More" text
<?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( '\]\]\>', ']]&gt;', $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' );
<?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