Last active
February 5, 2021 18:32
-
-
Save FerFuego/7e0c01c4b90c2b7cd65e3604e115e217 to your computer and use it in GitHub Desktop.
Trim text, strip shortcodes and excerpt return - Wordpress
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
/** | |
* Trim text, strip shortcodes and excerpt return | |
* | |
* @param Int $post - Post ID (optional) | |
* @param String $text - Text or get_the_conten() (optional) | |
* @param Int $words - Number of words to return | |
* | |
* @return string - "Ex: This is my text trim and..." | |
* | |
* Use: echo custom_trim_excerpt($post_id, '', 20 ); | |
* Use: echo custom_trim_excerpt('', get_the_content(), 20 ); | |
* Use: echo custom_trim_excerpt('', $my_text, 20 ); | |
* Print: "This is my text trim and..." | |
*/ | |
function custom_trim_excerpt ( $post = null, $text = null, $words = null ) { | |
if ( $post ) { | |
$content = get_the_content('', false, $post); | |
} | |
if ( $text ) { | |
$content = $text; | |
} | |
$content = excerpt_remove_blocks( $content ); | |
$content = apply_filters( 'the_content', $content); | |
$content = strip_shortcodes( $content ); | |
$content = str_replace( ']]>',']]>', $content); | |
if ($words) { | |
$content = wp_trim_words( $content, $words, '...' ); | |
} | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very picante! thanks!