WordPress.org

Support

Support » Themes and Templates » Don't strip certain tags in excerpt

Don't strip certain tags in excerpt

  • I have a magazine style layout all set up to automatically post a thumbnail, and a 100 word excerpt on the front page for each new article.

    However, one thing that I can’t figure out is the html tags. It’s stripping out html tags in the excerpt by default. This is generally a good one, but I want to leave some tags in there (like <p> for instance) without stripping them out.

    I tried this guide: http://www.aaronrussell.co.uk/blog/improving-wordpress-the_excerpt/

    It basically says to copy wp_trim_excerpt into the themes funcions.php file, rename it, and change the html stripping line to ignore <p> tags. However, when I do this the entire excerpt disappears.

    Any ideas on a way to accomplish what I’m looking for?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Whoops, forgot to post that the blog is

    http://batmanarkhamcity.org

    Btw, the code I put in functions.php that just made the whole excerpt disappear was:

    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'replacement_custom_trim_excerpt');
    
    function replacement_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);
    		$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('replacement_wp_trim_excerpt', $text, $raw_excerpt);
    }*

    I’m thinking you want

    function replacement_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);
    		$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 $text;
    	//return apply_filters('replacement_wp_trim_excerpt', $text, $raw_excerpt);
    }

    Yeah I actually tried that as well (the only difference was changing the return to $text, right?), and I get the same result where the excerpt doesn’t show up at all after doing it.

    Bump back to the first page, I’m still trying to figure this one out.

    Michael
    Forum Moderator

    @alchymyth

    100 word excerpt

    is not standard, so there is a possibility that your theme already uses a custom function or filter to show or change the excerpts.

    search throught functions.php of your theme or through the theme folder.

    is the_excerpt(); actually in the code of your front page?

    (in extreme, you could paste the front page code into a http://wordpress.pastebin.com/ and post the link to it here; same with functions.php)

    if this is a premium theme, it is a bit more difficult to help, as no-one can download the theme and check it.

    have you contacted the author? or do they have a help forum?

    The 100 word limit is my doing, I did it with the following statement in the functions.php file:

    function new_excerpt_length($length) {
    return 100;
    }
    add_filter(‘excerpt_length’, ‘new_excerpt_length’);

    Yeah, the_excerpt is called on my front page. Here is the pastebin for my functions.php file. Let me know if you want the front page as well.

    http://pastebin.com/3bsdjYNS

    I don’t believe that content_limit is called anymore. That was part of the theme but I changed the front page to call the_excerpt instead of it.

    Michael
    Forum Moderator

    @alchymyth

    there is a discrepancy between the filter you add and the function you defined (the function names do noot match):

    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'replacement_custom_trim_excerpt');
    
    function replacement_wp_trim_excerpt($text) {

    is that on purpose and is the function replacement_custom_trim_excerpt() somewhere else?

    also, this line in the code:
    $text = strip_tags($text);

    does not exclude the p tag from being stripped;
    try:

    $text = strip_tags($text,'<p>');

    http://php.net/manual/en/function.strip-tags.php

    Thank you!!

    No, I didn’t mean for the filter and function name to be different, and that fixed my problem. Apparently I had been staring at the code for far too long to actually see it 😛

    Thanks again!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Don't strip certain tags in excerpt’ is closed to new replies.