Created
September 30, 2016 19:15
-
-
Save zzap/e498bbc11a2ea573145721d2697c2ccc to your computer and use it in GitHub Desktop.
Trim post content down to exact number of words or characters.
This file contains hidden or 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 | |
/** | |
* 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