admin管理员组

文章数量:1130349

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?

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?

Share Improve this question asked Feb 15, 2014 at 14:55 Pieter GoosenPieter Goosen 55.5k23 gold badges117 silver badges211 bronze badges 1
  • I can't duplicate the problem. – s_ha_dum Commented Feb 15, 2014 at 15:14
Add a comment  | 

1 Answer 1

Reset to default 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.

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?

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?

Share Improve this question asked Feb 15, 2014 at 14:55 Pieter GoosenPieter Goosen 55.5k23 gold badges117 silver badges211 bronze badges 1
  • I can't duplicate the problem. – s_ha_dum Commented Feb 15, 2014 at 15:14
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: htmlExcerpt keep stripping ltagt tags