WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Do not strip html if excerpt is shorter (2 posts)

  1. juodikis
    Member
    Posted 1 year ago #

    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?

  2. juodikis
    Member
    Posted 1 year ago #

    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');

Topic Closed

This topic has been closed to new replies.

About this Topic