Skip to content

Instantly share code, notes, and snippets.

@zzap
Created September 30, 2016 19:15
Show Gist options
  • Save zzap/e498bbc11a2ea573145721d2697c2ccc to your computer and use it in GitHub Desktop.
Save zzap/e498bbc11a2ea573145721d2697c2ccc to your computer and use it in GitHub Desktop.
Trim post content down to exact number of words or characters.
<?php
/**
* Content trim characters
*
* Trim post content down to exact number of characters.
*
* @uses string_trim_characters()
*
* @param int $length Number of characters
* @param string $after Append to content
* @return string Returns trimmed content string
*/
function zzap_content_trim_characters( $length, $after = '...' ) {
$str = apply_filters( 'the_content', get_the_content() );
if ( ! ( strlen( $str ) <= $length ) ) {
$str = string_trim_characters( $str, $length, $after );
}
return $str;
}
/**
* Content trim words
*
* Trim post content down to exact number of words.
*
* @param int $length Number of words
* @param string $after Append to content
* @return string Returns trimmed content string
*/
function zzap_content_trim_words( $length, $after = '...' ) {
$str = apply_filters( 'the_content', get_the_content() );
if ( ! ( strlen( $str ) <= $length ) ) {
$str = wp_trim_words( $str, $length, $after );
}
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment