-1

On my category page there are list of 24 posts with title and excerpt content.

To display specific lengths of content, I am using substr function. Now the issue is that each post excerpt content are displaying in different lengths.

To display specific lengths of content I am using substr to cut string of specific length.

  $content = strip_tags(get_the_content());

  $content = substr($content, 0, 100);

  echo $content . '[..]';

for example:

      post title one
     post content size post content size post content size post content 
     size post content size[..]

   post title two
   post content 1'50" 0 size post content 55 size post content th[..]

   post title three
   post content size post "content 55" size checkk post content s[..]

Here in example you can see each post has different content lenght. Now what I am looking for is each post display specific lenght of content.

Second and third post content lengths should be same as first post. Also spelling should not cut.

For that I've tried

     $words = explode(' ', $string, ($word_limit + 1));

    if(count($words) > $word_limit) {

   array_pop($words);
  //add a ... at last article when more than limit word count
  echo implode(' ', $words)."..."; } else {
 //otherwise
 echo implode(' ', $words); }

but it is not working .

I need help to display same length content without word cut off.

  • Why are you not using the_excerpt() to display your content. Is there another issue with the_excerpt() – Pieter Goosen Apr 24 '14 at 10:38
  • not using the_excerpt because i am using the_excerpt on custom post type page to cut different size of content. that is why i am using substr for category page. – Jack Torris Apr 24 '14 at 10:58
  • do you mean to say if i had used the_except then all the post's content will display in same length and words will not cut off? – Jack Torris Apr 24 '14 at 11:02
  • Jip, but check my answer, more flexible – Pieter Goosen Apr 24 '14 at 11:07
0

I use wp_trim_words to create multiple excerpts. I always abuse it when I need more than one excerpt length. Here is how

function wpse_custom_excerpts($limit) {
    return wp_trim_words(get_the_excerpt(), $limit, '<a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&hellip;' . __( 'Read more &nbsp;&raquo;', 'wpse' ) . '</a>');
}

What this function do is taking get_the_excerpt trimming that to $limit set by the user and returning the text with a read more link at the end.

You can call this excerpt as follow in your template

echo wpse_custom_excerpts($limit);

where $limit will be your word count, so an excerpt of 30 word will be

echo wpse_custom_excerpts(30);

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.