Advertisement
alchymyth

content filter

Oct 4th, 2011
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.06 KB | None | 0 0
  1. add_filter('the_content', 'alc_autoshort_content');
  2. //a filter to turn the content of posts without the more-tag into excerpts///alchymyth 10/2011//
  3. function alc_autoshort_content( $content ) {
  4.     if( !is_singular() ) : // don't do it for single posts and pages
  5.     global $post;
  6.     if(!strpos($post->post_content,'<!--more')) {
  7.     // check for the 'more-tag' and leave posts with 'more-tag' unchanged
  8.         $text = do_shortcode( $content ); // otherwise make an excerpt
  9.         $text = strip_tags($text);
  10.         $excerpt_length = apply_filters('excerpt_length', 55);
  11.         $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
  12.         $words = explode(' ', $text, $excerpt_length + 1);
  13.         if (count($words)> $excerpt_length) {
  14.             array_pop($words);
  15.             $text = implode(' ', $words);
  16.             $text = $text . $excerpt_more;
  17.             // add the [...] or a possible link at the end of the shortened post
  18.         } else {
  19.             $text = implode(' ', $words) . $excerpt_more;
  20.             // add [...] or possible link even at the end of a short post
  21.         }
  22.         $content = $text;
  23.     }
  24.     endif;
  25. return $content;
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement