Display Links in Excerpts?

I’m having a problem with the_excerpt and can not find the answer anywhere…
I simply want to allow links to be clickable when they are displayed via the_excerpt! There has to be a function for this, rather than relying on a plugin. But I can’t find it and the advanced excerpt plugins are so complex that I am unable to find the small snippet which makes this work.

Related posts

Leave a Reply

4 comments

  1. You can use the script I found here:
    http://aaronrussell.co.uk/legacy/improving-wordpress-the_excerpt/

    I’ve modified it to show links in the excerpt, and removed some of the other functions:

    <?php
    function keep_my_links($text) {
      global $post;
    if ( '' == $text ) {
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]&gt;', $text);
        $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
        $text = strip_tags($text, '<a>');
      }
      return $text;
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'keep_my_links');
    ?>
    

    The part that fixes it is $text = strip_tags($text, '<a>');. along with remove_filter('get_the_excerpt', 'wp_trim_excerpt');

  2. The code basically allows a list of comma separated HTML tags to work in your excerpts which are normally stripped by WordPress. Tested on Genesis and works.

     add_filter( 'get_the_content_limit_allowedtags', 'get_the_content_limit_custom_allowedtags' );
    
    function get_the_content_limit_custom_allowedtags() {
    // Add custom tags to this string
    return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>'; 
    }
    

    Source http://daan.kortenba.ch/add-tags-to-genesis-content-limit-in-content-archives/

  3. WordPress strips out tags in wp_trim_words(), which is called by get_the_excerpt(); so we have to filter ‘wp_trim_words’, basically copying that function with one change: replace wp_strip_all_tags() with strip_tags().

    We don’t want other functions that run wp_trim_words to be modified, so we add our filter while get_the_excerpt() is running, and remove it when we’re done.

    // Allow links in excerpts
    function sg_trim_words( $text, $num_words, $more, $original_text ) {
        $text = strip_tags( $original_text, '' );
        // @See wp_trim_words in wp-includes/formatting.php
        if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && preg_match( '/^utf-?8$/i', get_option( 'blog_charset' ) ) ) {
            $text = trim( preg_replace( "/[nrt ]+/", ' ', $text ), ' ' );
            preg_match_all( '/./u', $text, $words_array );
            $words_array = array_slice( $words_array[0], 0, $num_words + 1 );
            $sep = '';
        } else {
            $words_array = preg_split( "/[nrt ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
            $sep = ' ';
        }
        if ( count( $words_array ) > $num_words ) {
            array_pop( $words_array );
            $text = implode( $sep, $words_array );
            $text = $text . $more;
        } else {
            $text = implode( $sep, $words_array );
        }
        // Remove self so we don't affect other functions that use wp_trim_words
        remove_filter( 'wp_trim_words', 'sg_trim_words' );
        return $text;
    }
    // Be sneaky: add our wp_trim_words filter during excerpt_more filter, which is called immediately prior
    function sg_add_trim_words_filter( $excerpt_length ) {
        add_filter( 'wp_trim_words', 'sg_trim_words', 10, 4 );
        return $excerpt_length;
    }
    add_filter( 'excerpt_more', 'sg_add_trim_words_filter', 1 );

    I wrote this gist after reviewing other suggested methods, because I think this is a more targeted solution. The gist will be updated going forward.