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(']]>', ']]>', $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() ) . '">' . '…' . __( 'Read more about this article <span class="meta-nav">→</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(']]>', ']]>', $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() ) . '">' . '…' . __( 'Read more about this article <span class="meta-nav">→</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 can't duplicate the problem. – s_ha_dum Commented Feb 15, 2014 at 15:14
1 Answer
Reset to default 1add_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(']]>', ']]>', $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() ) . '">' . '…' . __( 'Read more about this article <span class="meta-nav">→</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(']]>', ']]>', $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() ) . '">' . '…' . __( 'Read more about this article <span class="meta-nav">→</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 can't duplicate the problem. – s_ha_dum Commented Feb 15, 2014 at 15:14
1 Answer
Reset to default 1add_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
版权声明:本文标题:html - Excerpt keep stripping <a> tags 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749159512a2325156.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论