Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! function_exists( 'truncate_post' ) ) {
  4.  
  5. function truncate_post( $amount, $echo = true, $post = '', $strip_shortcodes = false ) {
  6. global $shortname;
  7.  
  8. if ( '' == $post ) global $post;
  9.  
  10. $post_excerpt = '';
  11. $post_excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
  12.  
  13. if ( 'on' == et_get_option( $shortname . '_use_excerpt' ) && '' != $post_excerpt ) {
  14. if ( $echo ) echo $post_excerpt;
  15. else return $post_excerpt;
  16. } else {
  17. // get the post content
  18. $truncate = $post->post_content;
  19.  
  20. // remove caption shortcode from the post content
  21. $truncate = preg_replace( '@\[caption[^\]]*?\].*?\[\/caption]@si', '', $truncate );
  22.  
  23. // remove post nav shortcode from the post content
  24. $truncate = preg_replace( '@\[et_pb_post_nav[^\]]*?\].*?\[\/et_pb_post_nav]@si', '', $truncate );
  25.  
  26. // Remove audio shortcode from post content to prevent unwanted audio file on the excerpt
  27. // due to unparsed audio shortcode
  28. $truncate = preg_replace( '@\[audio[^\]]*?\].*?\[\/audio]@si', '', $truncate );
  29.  
  30. // Remove embed shortcode from post content
  31. $truncate = preg_replace( '@\[embed[^\]]*?\].*?\[\/embed]@si', '', $truncate );
  32.  
  33. if ( $strip_shortcodes ) {
  34. $truncate = et_strip_shortcodes( $truncate );
  35. } else {
  36. // apply content filters
  37. $truncate = apply_filters( 'the_content', $truncate );
  38. }
  39.  
  40. // decide if we need to append dots at the end of the string
  41. // if ( strlen( $truncate ) <= $amount ) {
  42. // $echo_out = '';
  43. // } else {
  44. // $echo_out = '...';
  45. // // $amount = $amount - 3;
  46. // }
  47.  
  48. // trim text to a certain number of characters, also remove spaces from the end of a string ( space counts as a character )
  49. $truncate = rtrim( et_wp_trim_words( $truncate, $amount, '' ) );
  50.  
  51. // remove the last word to make sure we display all words correctly
  52. if ( '' != $echo_out ) {
  53. $new_words_array = (array) explode( ' ', $truncate );
  54. array_pop( $new_words_array );
  55.  
  56. $truncate = implode( ' ', $new_words_array );
  57.  
  58. // append dots to the end of the string
  59. $truncate .= $echo_out;
  60. }
  61.  
  62. if ( $echo ) echo $truncate;
  63. else return $truncate;
  64. };
  65. }
  66.  
  67. }
  68.  
  69.  
  70. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement