WordPress.org

Support

Support » How-To and Troubleshooting » Using WP_TRIM_WORDS and THE_CONTENT

Using WP_TRIM_WORDS and THE_CONTENT

  • 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 ); ?>
        </div>

    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?

    Thanks
    dvent

Viewing 2 replies - 1 through 2 (of 2 total)
  • Andrew Bartel

    @andrew-bartel

    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?

    vtxyzzy

    @vtxyzzy

    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); ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using WP_TRIM_WORDS and THE_CONTENT’ is closed to new replies.
Skip to toolbar