Support » Themes and Templates » Do not strip html if excerpt is shorter

  • Resolved juodikis

    (@juodikis)


    Hi,

    Usually I find everything on this forum, but not this problem ๐Ÿ™‚
    I’m trying to do one thing. In categories page I use excerpt (length 100). But I’m trying to do that if post length is less than 100 it would show full post (with all html tags).
    Is it possible to do? How?

Viewing 1 replies (of 1 total)
  • OK, I figured out. Put this into functions.php

    function my_custom_excerpt($text) {
    	global $post;
    	if ( '' == $text ) {
    		$text = get_the_content('');
    		$text = apply_filters('the_content', $text);
    		$text = str_replace('\]\]\>', ']]>', $text);
    		$fulltext = str_replace('\]\]\>', ']]>', $text);
    		$text = strip_tags($text, '<p>');
    		$excerpt_length = 100;
    		$words = explode(' ', $text, $excerpt_length + 1);
    		if (count($words) > $excerpt_length) {
    			array_pop($words);
    			array_push($words, '... <a href="'. esc_url( get_permalink() ) . '">Read more ยป</a>');
    			$text = implode(' ', $words);
    		}
    		else {
    			$text = $fulltext;
    		}
    	}
    	return $text;
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'my_custom_excerpt');

Viewing 1 replies (of 1 total)
  • The topic ‘Do not strip html if excerpt is shorter’ is closed to new replies.