ThemeShaper Forums » Thematic

[closed]

How to make excerpts roll over and say Uncle

(11 posts)
  • Started 3 years ago by helgatheviking
  • Latest reply from kim4true
  • This topic is not a support question

Tags:

  1. So I have been extremely dissatisfied with the handling of excerpts and I went looking for solutions to how to use different length excerpts in different places. I cam across a php Class written by Baelor Rae on stackoverflow.com. It worked great, but I thought it could go further.

    So i modified it to also change the [...] text, add a permalink and tell it which html tags you would like allowed.

    instead of using the_excerpt() you'd use kia_excerpt() (i also made a get_kia_excerpt() that returns the value.... with the following parameters

    length- can be passed short (25), regular(55), long(100) xlong (200) or any custom number

    a truncation character/text- the wordpress default is 'regular' [...], none, and ellipse. you can also pass in a custom text that will be turned into a permalink to the rest of the post.

    and

    strip tags- the WP default is to strip everything, but you can pass in <a><strong> or hopefully whatever tags you'd like left

    so in sample usage you might use:

    kia_excerpt('short','Read More','<strong>');

    to get short excerpts with a permalink and bold tags allowed.

    to get an excerpt of 15 words, you'd use kia_excerpt(15) and so on

    i'd like to make it sentence aware for version 1.0, but that could be a bit beyond me. there is a plugin that already does that so i might be able to borrow and blend.

    i haven't tested it extensively, so let me know if you come across any problems or can think of any improvements.

    you'd either paste it all into your functions.php file (or i usually keep it separate and then just include the file from my functions.php file as things tend to get a little crowded in functions.php). without further blathering, i present the kia_excerpt

    <?php
    
    /*-----------------------------------------------------------------------------------*/
    /* KIA TOTAL CONTROL OF EXCERPTS
    /* by Kathy Darling
    /* www.kathyisawesome.com (coming eventually)
    /*-----------------------------------------------------------------------------------*/
    
    //Replace wp_trim_excerpt with addition of filter for strip_tags
    function improved_trim_excerpt($text) {
    	$raw_excerpt = $text;
    	if ( '' == $text ) {
    		$text = get_the_content('');
    
    		$text = strip_shortcodes( $text );
    
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    
    		$tags = apply_filters('strip_tags', NULL);
    
    		$text = strip_tags($text, $tags);
    		$excerpt_length = apply_filters('excerpt_length', 55);
    		$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    		$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    		if ( count($words) > $excerpt_length ) {
    			array_pop($words);
    			$text = implode(' ', $words);
    			$text = $text . $excerpt_more;
    		} else {
    			$text = implode(' ', $words);
    		}
    	}
    	return apply_filters('improved_trim_excerpt', $text, $raw_excerpt);
    }
    
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'improved_trim_excerpt');
    
    /* Class that enables excerpt length parameter */
    /* called via kia_excerpt('length') */
    
    class Excerpt {
    
      // Default length (by WordPress)
      public static $length = 55;
    
       // Default more (by WordPress)
      public static $more = '[...]';
    
       // Default tags (none)
      public static $tags = '';
    
      // So you can call: kia_excerpt('short');
      public static $types = array(
          'short' => 25,
          'regular' => 55,
          'long' => 100,
    	  'xlong' => 200,
        );
    
    	// So you can call: kia_excerpt('short','ellipse');
        public static $more_types = array(
    	  'none' => '',
          'regular' => '[...]',
          'ellipse' => '...',
        );
    
      /**
       * Sets the length for the excerpt,
       * then it adds the WP filter
       * And automatically calls the_excerpt();
       *
       * @param string $new_length
       * @return void
       * @author Baylor Rae
       */
      public static function filter($new_length = 55, $new_more='[...]',$new_tags='') {
        Excerpt::$length = $new_length;
    	Excerpt::$more = $new_more;
    
    	Excerpt::$tags = $new_tags;
    
    	add_filter('strip_tags', 'Excerpt::new_tags');
        add_filter('excerpt_length', 'Excerpt::new_length',98);
    	add_filter('excerpt_more', 'Excerpt::new_more',99);
    
    	return Excerpt::output();
    
      }
    
      // Tells WP the new length
      public static function new_length() {
        if( isset(Excerpt::$types[Excerpt::$length]) ){
          return Excerpt::$types[Excerpt::$length];
        } else {
          return Excerpt::$length;
    	}
      }
    
       // Tells WP the new more
      public static function new_more() {
    
        if( isset(Excerpt::$more_types[Excerpt::$more]) ) {
          return Excerpt::$more_types[Excerpt::$more];
        } else {
    		$db = new ReadMore(Excerpt::$more);
    		return $db->readmore(Excerpt::$more);
    	}
    }
    
        // Tells WP the new strip tags
      public static function new_tags() {
          return Excerpt::$tags;
      }
    
      // Echoes out the excerpt
      public static function output() {
        return get_the_excerpt();
      }
    
    }
    
    // An alias to the class
    function get_kia_excerpt($length = 55, $more='[...]', $tags='') {
      return Excerpt::filter($length, $more, $tags);
    }
    
    // An alias to the class
    function kia_excerpt($length = 55, $more='[...]', $tags='') {
      echo get_kia_excerpt($length, $more, $tags);
    }
    
    class ReadMore {
      private $text;
    
      public function __construct($text) {
    	$temp = "..." . '<a class="readmore" title="'. _('Permalink to').get_the_title() . '" href=" ' . get_permalink() . '">'._($text).'</a>';
    	$this->more = $temp;
    
      }
      public function readmore($text) {
        return $this->more;
      }
    }

    to then use this in thematic and swap out all the default excerpts for my kia_excerpt you'd do this:

    //change default Thematic excerpts to kia_excerpts
    function childtheme_excerpts($post) {
    	global $thematic_content_length;
    		if ( strtolower($thematic_content_length) == 'excerpt' ) {
    	$post = '';
    	$post .= get_kia_excerpt('short','ellipse','<strong><em><a>');
    	}
    	return $post;
    }
    
    add_filter('thematic_post', 'childtheme_excerpts');
    Posted 3 years ago #
  2. cannobbio
    Member

    This is great!
    Is there any way I can keep the "entry-thumb" feature and wrap the excerpt y a paragraph?

    Thank you =)

    Posted 3 years ago #
  3. cannobbio
    Member

    I was looking at your site http://www.weighttrainingforwomentoday.com and noticed you missed a semicolon in "&raquo", on the read-more link..."Continue reading »".

    cya

    Posted 3 years ago #
  4. of course it it. you'd swap out

    $post = '';
    $post .= get_kia_excerpt('short','ellipse','<strong><em><a>';

    for whatever thematic has for the function in the content-extensions.php file. i don't have it in front of me so you will have to do the research yourself. and then just replace get_the_excerpt with get_kia_excerpt. to wrap in a paragraph tag i think you'd just do

    $post .= "<p>" . get_kia_excerpt('short','ellipse','' . "</p>";

    with that $post variable you are building what the excerpt looks like and so you could make it say anything you wanted there.

    if you changed it to

    $post = "bacon";

    then all your excerpts would say bacon.

    thanks for the tip on my site, it is about a year old and pretty much defunct. if i ever get my portfolio finished then i will update my profile.

    Posted 3 years ago #
  5. cannobbio
    Member

    Thank you!
    And thank you for all your other posts, they have been very helpfull!

    // Change default Thematic excerpts to kia_excerpts
    function childtheme_excerpts($post) {
    	global $thematic_content_length;
    		if ( strtolower($thematic_content_length) == 'excerpt' ) {
    	$post = '';
    	$post .= "<p>" . get_kia_excerpt('short','Ver más »','') . "</p>";
    	if ( apply_filters( 'thematic_post_thumbs', TRUE) ) {
    				$post_title = get_the_title();
    				$size = apply_filters( 'thematic_post_thumb_size' , array(100,100) );
    				$attr = apply_filters( 'thematic_post_thumb_attr', array('title'	=> 'Permalink to ' . $post_title) );
    				if ( has_post_thumbnail() ) {
    					$post = '' . get_the_post_thumbnail(get_the_ID(), $size, $attr) . '' . $post;
    					}
    			}
    	}
    	return $post;
    }
    add_filter('thematic_post', 'childtheme_excerpts');
    Posted 3 years ago #
  6. cannobbio
    Member

    Hi @helgatheviking
    I'm using your function and was wondering if I could force it to trim custom written excerpts and not only autommatic excerpts. I need all posts in archive pages to be trimmed and have a "Read more" link regardless if they are custom or autommatic. I also need to force the "read more" link if the character count of the post is lower than the kia $lenght and in custom written excerpts.

    In other words, I need all entries in archive pages to have a trimmed excerpt and a "Read more" link. ¿Is that possible?.

    Thank you in advance.

    Posted 3 years ago #
  7. well i truly believe that everything is possible... it is just a matter of how much time you want to invest in solving the problem.

    but unfortunately i don't know how to solve the problem you are describing.

    Posted 3 years ago #
  8. cannobbio
    Member

    Oh, ok =(
    I thought maybe you knew how to apply this same function to written excerpts.
    If you got any clue...I'm all ears.

    Thank you.

    Posted 3 years ago #
  9. middlesister
    Member

    I have done something similar to what you described. I ended up completely bypassing the_excerpt and writing my own custom code. It is basically a modified copy of wp_trim_excerpt, but I included the code straight in the loop (a small custom loop). A bit hacky, and probably should be moved to a proper function of its own when I get around to it.

    This is my code - which is placed straight in the loop where you normally call the_excerpt()

    // make a custom shortened excerpt just for this listing
    $content = get_the_excerpt();
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
    $content = strip_tags($content);
    $excerpt_length = apply_filters('excerpt_length', 80);
    $words = explode(' ', $content, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
    	array_pop($words);
    	array_push($words, '[...]');
    	$content = implode(' ', $words);
    }
    echo $content;
    echo '<a class="more-link" href="'. get_permalink() . '">' .__('Continue reading &raquo;', 'thematic') . '</a>';

    Since I use get_the_excerpt(), I get the resulting text from wordpress - either the manually written excerpt or if there is none, the automatically generated one. This code trims the result down to 80 words, no matter what kind of excerpt, adds the […] part to the end if the text is longer than specified, and adds a custom read more link.

    Like you, I was annoyed at the big difference between manual excerpts and the generated ones. I wanted to display some posts in columns that were of more or less the same height. A very long written excerpt would totally break the layout. So I ended up with this.

    Posted 3 years ago #
  10. cannobbio
    Member

    Thank you @middlesister !
    It's what I'm looking for, to have all entries look the same. I'll try to use your code but I think you're right, it needs to be a function. I'll see what I can do.

    Thank you again!

    Posted 3 years ago #
  11. Thanks to everyone on this post... You've really helped me with my excerpt problems.

    Posted 2 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.