• Hi all, I’m looking for help with some code I’m using to customize my excerpts. I’m not an expert in writing custom functions, just an expert on Googling until I find the answer I’m looking for. 🙂

    I’m using the following code to allow links in excerpts:

    // Add links to excerpts
    function new_wp_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);
    		$text = strip_tags($text, '<a>');
    		$excerpt_length = apply_filters('excerpt_length', 82);
    		$words = preg_split('/(<a.*?a>)|\n|\r|\t|\s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
    		if ( count($words) > $excerpt_length ) {
    			array_pop($words);
    			$text = implode(' ', $words);
    			$text = $text . $excerpt_more;
    		} else {
    			$text = implode(' ', $words);
    		}
    	}
    	return apply_filters('new_wp_trim_excerpt', $text, $raw_excerpt);
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'new_wp_trim_excerpt');

    In addition to this, I’m interested being able to customize the excerpt length on the fly when I call get_the_excerpt() in my loop. I’ve tried some of the functions on the following page, but I think they are conflicting with the code I already have for allowing HTML links in excerpts:

    http://wordpress.org/support/topic/limit-excerpt-length-by-characters

    Does anyone know of an easy way to allow links AND custom excerpt lengths in one function? In my situation, I have a “featured post” box on my index.php page, and in that one post I want the excerpt length to be different than all the others.

    Thanks in advance for reading!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Jenn

    (@jenny_jane)

    Okay, here’s what I’m doing to achieve what I want. functions.php:

    // Custom excerpt lengths
    function get_excerpt($count){
    	$permalink = get_permalink($post->ID);
    	$excerpt = get_the_content();
    	$excerpt = strip_tags($excerpt, '<a>');
    	$excerpt = strip_shortcodes( $excerpt );
    	$excerpt = substr($excerpt, 0, $count);
    	$excerpt = $excerpt;
    	if ( count($words) > $excerpt_length ) {
    			array_pop($words);
    			$text = implode(' ', $words);
    			$text = $text . $excerpt_more;
    		} else {
    			$text = implode(' ', $words);
    		}
    	return $excerpt;
    }
    
    // Remove ...
    function trim_excerpt($text) {
    	return rtrim($text,'[...]');

    Then in the index.php file:

    <p><?php echo get_excerpt('600'); ?> &hellip; <a href="<?php the_permalink() ?>">Read More</a></p>

    Some notes: The client wants to always see “Read More” even if the excerpt displays the entire text of the post.

    It’s working for me, but open to hearing some thoughts if there are cleaner ways to do it. Thanks!

    I found that using this worked, but certain posts had odd non-UTF8 characters that somehow got through to the content being output to the browser. Using this

    $text = get_the_content('');
    $text = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $text);

    stripped non-UTF8 before they enter any other parsing steps.
    Cheers

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Excerpts – have more than one length AND include certain tags’ is closed to new replies.