Created
August 8, 2014 20:43
-
-
Save intelliweb/d4eccf2d4ab625ce5d8f to your computer and use it in GitHub Desktop.
WP: Custom length for content, excerpt, title, etc. using WP's wp_trim_words function
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 | |
/* Trim content, excerpt, or any other string of text to a specified number of words */ | |
wp_trim_words( $text, $num_words = 55, $more = '...' ); | |
/* Custom function to trim the title. Use in place of get_the_title() | |
Example usage showing max 10 words of the title: <?php echo intw_trim_title(10); ?> */ | |
function intw_trim_title($n) { | |
return wp_trim_words( get_the_title(), $n ); | |
} | |
/* Custom function to trim the excerpt. Use in place of get_the_excerpt() | |
Example usage showing max 20 words of the excerpt: <?php echo intw_trim_excerpt(20); ?> */ | |
function intw_trim_excerpt($n) { | |
return wp_trim_words( get_the_excerpt(), $n ); | |
} | |
/* Custom function to trim the content. Use in place of get_the_content() | |
Example usage showing max 100 words of the content: <?php echo intw_trim_content(100); ?> */ | |
function intw_trim_content($n) { | |
return wp_trim_words( get_the_content(), $n ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment