Advertisement
bananamariap

Limit Text Content

May 12th, 2021 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. /**
  2. * Limit words of a content
  3. * $post_id: pass the post id
  4. * $limit: int ( default 50 )
  5. * use: get_intro_text ( get_the_ID(), 100 );
  6. */
  7.  
  8. function get_intro_text( $post_id, $limit = 50 )
  9. {
  10. $intro_text = get_the_content($post_id);
  11.  
  12. $intro_text = preg_replace(" ([.*?])",'',$intro_text);
  13.  
  14. $intro_text = strip_shortcodes($intro_text);
  15. $intro_text = strip_tags($intro_text);
  16. $intro_text = substr($intro_text, 0, $limit);
  17. $intro_text = substr($intro_text, 0, strripos($intro_text, " "));
  18. $intro_text = trim(preg_replace( '/\s+/', ' ', $intro_text));
  19. $intro_text = $intro_text.'... <a href="'.get_the_permalink( $post_id ).'">read more</a>';
  20. return $intro_text;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement