• Resolved marques_uk

    (@marques_uk)


    Hi everyone,

    I’m putting together a website on my local server, the home page consists of the latest post, but I only want to show the except.

    I’ve used the_excerpt function to do this, however it strips out all the tags i.e. <p>, </br> etc…

    Does anyone have a customised excerpt that preserves the tags? I’ve increased the length of the excerpt to 140 so looks a bit rubbish without any formatting.

    I have looked around, but can’t find any that seem to work with the latest version of WP.

    Thanks in advance,
    marques_uk

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    It’s tricky… the default filters for the_excerpt are these:

    add_filter( 'the_excerpt',     'wptexturize'      );
    add_filter( 'the_excerpt',     'convert_smilies'  );
    add_filter( 'the_excerpt',     'convert_chars'    );
    add_filter( 'the_excerpt',     'wpautop'          );
    add_filter( 'the_excerpt',     'shortcode_unautop');
    add_filter( 'get_the_excerpt', 'wp_trim_excerpt'  );

    I’ve played with removing them, but it doesn’t quite do what you want which is preserve the HTML tags.

    It may be I’m just not good at playing with those filters. ??

    Give the plugin repo a look, one of these might do it for you.

    Thread Starter marques_uk

    (@marques_uk)

    Hi Jan,

    Thanks for your comments. I have thought about using a plugin but I’d rather add a new function rather than relying on another plugin, if I can’t find a solution I’ll use this option.

    There’s quite a few articles on the web that show how you can do it, but I’m not sure if they are outdated as I can’t seem to get them work.

    I came across this one https://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-preserve-html-tags-in-wordpress-excerpt-without-a-plugin/

    Think it might need tweaking but not sure where to start. Would you mind taking a look to see if you can get it to work for you?

    Many thanks

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    I tried that code with my Twenty Eleven child theme but couldn’t get it to work for me. Likely, I’m missing something basic.

    If you know how to work with the_content in PHP then you have lots of options but as I’d mentioned, it is tricky.

    If your theme is using the_excerpt you can start with the_content like so and make your edits.

    add_filter( 'the_excerpt' , 'mh_excerpt_to_content'  );
    function mh_excerpt_to_content( $content ) {
            $content = get_the_content();
            // $content = apply_filters('the_content', $content);
            // Something interesting HERE
            return $content;
    }

    But the interesting part is where I get stuck. If you just truncate the_content then odds are you’ll do so within the html tag and that will render your output badly.

    Or you can explore the plugins. That’s where I would start. ??

    Thread Starter marques_uk

    (@marques_uk)

    I’m putting together my own theme, I’m using the current twentyten functions to save me some work and I amend if necessary.

    On the front page, because of the design I want to have an except of the main home page. Do you know how I would show an excerpt of a page? Would I just use the_excerpt() function again?

    Then I have the latest post, and again only showing the except as mentioned. I’d like both to include HTML tags.

    This is for a client who will be updating the website themselves so need to make it as simple as possible, i.e. They only need to put content into the main home page and add a post and excerpts will show on the front page.

    If anyone knows how to do this, be much appreciated. I’ll try to find a solution and if I do I’ll post on here.

    Thanks for trying to help Jan, I’ll probably end up using the plugin you mentioned if I can’t find a solution this weekend.

    Thread Starter marques_uk

    (@marques_uk)

    Hi everyone,

    I used the https://www.ads-software.com/extend/plugins/advanced-excerpt/ to prevent excerpt HTML tags from being stripped. Works perfectly saves having to edit the core files anyway.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘the_excerpt() function modification’ is closed to new replies.