Home / excerpt / Use wp_trim_words to trim your text in wordpress
  • How to Use wp_trim_words to trim your text in wordpress


    WordPress 3.3+ has a core function called wp_trim_words() this function will trim text to a specified number of words and return the result. Adding this snippet to your wordpress loop will let you easily trim the number of words displayed within the_content, the_excerpt, the_title or any other value you pass to this core function.

    ( click code to copy )

    wordpress snippet : PHP

    echo wp_trim_words( get_the_content(), 100 ); // post content
    echo wp_trim_words( get_the_excerpt(), 100 ); // post excerpt
    echo wp_trim_words( get_the_title(), 100 ); // post title
    ( click code to copy )

    wordpress snippet : PHP: Example

    $trimmed = wp_trim_words( $text, $num_words = 55, $more = null );


    (string) (required) Text to trim
    Default: None
    (integer) (optional) Number of words
    Default: 55
    (string) (optional) What to append if $text needs to be trimmed.
    Default: '&hellip;'

    ( WordPress codex functions, hooks, in this snippet. )

    get_the_excerpt, the_content, the_excerpt, the_title, get_the_content, get_the_title, wp_trim_words, _e, _ex, wp,
  • Kamil Janicki

    Hello, thanks for all your useful advice. I have one question, not regarding the current post, but your webpage theme. Could you share with us (or maybe you have done so before, but I can’t find it?) how this great categories/tag listing in the sidebar is made? It looks really great and I wondered how to create a similar listing – with “progress bars” showing how many posts were publisher in a given category/tag.

  • wordpress has lots in its upgraded versions..