Using WP_TRIM_WORDS and THE_CONTENT (3 posts)

  1. dvent
    Posted 3 years ago #

    Hi All,

    I'm trying to add content from a page using the following code

    <div class="right-news">
        	<?php wp_trim_words( the_content, $num_words = 20, $more = null ); ?>

    I cannot use the_excerpt as I've set a length for this elsewhere of 8 words.

    I want to show the first 20 words of the content in this particular area, but the code used above isn't working.

    Anyone spot the error?


  2. Andrew Bartel
    Posted 3 years ago #

    When you pass parameters to a function in php, you don't actually set the variable, just pass the values. Your example:
    echo wp_trim_words( the_content, 20, null )

    Make sense?

  3. vtxyzzy
    Posted 3 years ago #

    I think this is what you want:

    <?php echo wp_trim_words( get_the_content(), $num_words = 20, $more = null ); ?>

    Setting the variables is OK, although not required.

    However, you might run into trouble if wp_trim_words trims in the middle of some HTML tags. In that case, this might help:

    <?php echo balanceTags(wp_trim_words( get_the_content(), $num_words = 20, $more = null ), true); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic