0

I have the following function to display my excerpt

  <?php function pietergoosen_custom_wp_trim_excerpt($text) {
global $post;
$raw_excerpt = $text;
if ( '' == $text ) {
    $text = get_the_content('');

    $text = strip_shortcodes( $text );

    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]&gt;', $text);

    //Add the allowed HTML tags separated by a comma.
    $allowed_tags = '<p>,<a>,<em>,<strong>,<img src />,<audio><video>';  
    $text = strip_tags($text, $allowed_tags);

    //Change the excerpt word count.
    $excerpt_word_count = 105; 
    $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); 

    //Change the excerpt ending.
    $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&hellip;' . __( 'Read more about this article <span class="meta-nav">&rarr;</span>', 'pietergoosen' ) . '</a>'; 
    $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);

    $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('wp_trim_excerpt', $text, $raw_excerpt);
}

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'pietergoosen_custom_wp_trim_excerpt'); ?>

My problem is, my <a> tags still get stripped. I followed a couple examples, and all suggest adding the <a> tag to the $allowed_tags, but even that doesn't solve the problem. Any suggestions?

1
  • I can't duplicate the problem. – s_ha_dum Feb 15 '14 at 15:14
1
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>'; 
}

You can also add this code in your functions file and customize which tags you want to allow in excerpts.

3
  • Thanks Brad, I can see the <a> now, just one problem. Although I can see the <a> tag, I can't see the image inside. Is there more to images than just the <a> tag – Pieter Goosen Feb 15 '14 at 15:59
  • Got the <img> tag wrong. Used <img src /> – Pieter Goosen Feb 15 '14 at 18:05
  • Brad, just one thing I forgot to add last night, you don't need the add_filter( 'get_the_content_limit_allowedtags', 'get_the_content_limit_custom_allowedtags' ); part. You want to add to a filter that doesn't exist. Your code works just fine without that piece of code – Pieter Goosen Feb 16 '14 at 7:18

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.