我有以下功能来显示我的摘录
<?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\'); ?>
我的问题是
<a>
标签仍然会被剥离。我举了几个例子,都建议添加
<a>
标记到
$allowed_tags
, 但即使这样也不能解决问题。有什么建议吗?
最合适的回答,由SO网友:Brad Dalton 整理而成
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>\';
}
您还可以将此代码添加到函数文件中,并自定义要在摘录中允许的标记。