5

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.

11

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
  • what does if ( '' == $text ) exactly do?
    – MrKainig
    Mar 19 '16 at 13:22
  • Unfortunately the link to Aaron Russell is no longer working and there is an error in the pagination of that site on the articles page that keeps you from seeing all the pages of articles :( Jun 19 '17 at 17:18
1

You can use the following plugin for allowing links and other html tags in the excerpts

Plugin: Show links in excerpts wordpress

I have no affiliation regarding the plugin

4
  • Please state you affiliation regarding the plugin May 26 '14 at 6:38
  • I use advanced excerpts wordpress plugin from the first day I started running my blog: weblogcode. It's very user friendly. It's not been updated but it's compatible with wordpress 3.9. I hope the author of advanced excerpts plugin will update it soon. I am just a basic wordpress user for that plugin. May 26 '14 at 6:50
  • I have updated your answer to show your affiliation. May 26 '14 at 6:55
  • 1
    Moreover an answer should be more than just a link to an external site. Please add an explanation: why could that solve the problem?
    – Sven
    May 26 '14 at 7:10
1

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
  • 1
    Brad, would you please add a small description to what your code does and how it works. Dumping code is not a constructive answer May 26 '14 at 6:40
  • Happy to Pieter. 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. May 26 '14 at 7:00
  • 1
    This will only work on sites that use Genesis, and even then only with particular settings in place. Jun 6 '18 at 23:21
0

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( "/[\n\r\t ]+/", ' ', $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( "/[\n\r\t ]+/", $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.

1
  • 1
    @cybmeta Good point. I've edited my answer. Jun 7 '18 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.