2

OK, this might be a duplicate, I was just unable to put my idea in proper search terms, sorry for that!

Is there a built-in function or template tag to get the first n words of the content? I mean the_content().

Thanks!

  • 1
    You are looking for the excerpt. I done a complete answer on that. Have a look here – Pieter Goosen Apr 16 '14 at 14:35
7

Yep, wp_trim_words():

 <?php $trimmed = wp_trim_words( $text, $num_words = 55, $more = null ); ?>

Or in your case:

<?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 55 ) ); ?>
  • @JaromírtheGreenhorn If you'd like to use this on get_the_content(), you'll need to strip HTML beforehand (see revision). – TheDeadMedic Apr 16 '14 at 16:01
  • It seems to work for me without extra strip down, but good to know. – Jaromír the Greenhorn Apr 16 '14 at 18:52
  • 2
    I would still strongly advise that you use it - it might be working now, but eventually you'll end up with malformed HTML. – TheDeadMedic Apr 17 '14 at 8:50
  • (ironic) Alright, Master, I promise to be obedient! – Jaromír the Greenhorn Apr 17 '14 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.