Advertisement
rdusnr

Untitled

Nov 22nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.49 KB | None | 0 0
  1.  
  2. if ( ! function_exists( 'kleo_new_excerpt_length' ) ) {
  3.     function kleo_new_excerpt_length($length) {
  4.         return 60;
  5.     }
  6.     add_filter('excerpt_length', 'kleo_new_excerpt_length');
  7. }
  8.  
  9. if ( ! function_exists( 'kleo_excerpt' ) ) {
  10.     function kleo_excerpt( $limit = 20, $words = true ) {
  11.  
  12.         $from_content = false;
  13.         $excerpt_initial = get_the_excerpt();
  14.  
  15.         if( $excerpt_initial == '' ) {
  16.             $excerpt_initial = get_the_content();
  17.             $from_content = true;
  18.         }
  19.         $excerpt_length = apply_filters( 'excerpt_length', $limit );
  20.         $excerpt_initial = preg_replace( '`\[[^\]]*\]`', '', $excerpt_initial );
  21.         $excerpt_initial = strip_tags( $excerpt_initial );
  22.         $excerpt_initial = mb_substr( $excerpt_initial, 0, $excerpt_length );
  23.  
  24.  
  25.         /* If we got it from get_the_content -> apply length restriction */
  26.         if ( $from_content ) {
  27.             $excerpt_length = apply_filters( 'excerpt_length', $limit );
  28.             $excerpt_initial = wp_trim_words( $excerpt_initial, $excerpt_length, '' );
  29.         }
  30.  
  31.         if ( $words ) {
  32.             $excerpt = explode( ' ', $excerpt_initial, $limit );
  33.             if ( count( $excerpt ) >= $limit ) {
  34.                 array_pop( $excerpt );
  35.                 $excerpt = implode( " ", $excerpt ) . '...';
  36.             } else {
  37.                 $excerpt = implode( " ", $excerpt ) . '';
  38.             }
  39.         } else {
  40.             $excerpt = substr( $excerpt_initial, 0, $limit ) . ( strlen( $excerpt_initial ) > $limit ? '...' : '' );
  41.         }
  42.  
  43.         return '<p>' . $excerpt . '</p>';
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement