Skip to content

Instantly share code, notes, and snippets.

@intelliweb
Created August 8, 2014 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intelliweb/d4eccf2d4ab625ce5d8f to your computer and use it in GitHub Desktop.
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
<?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