Wordpress html标签在手动摘录中剥离(Wordpress html tags strip on manual excerpt)

编程入门 行业动态 更新时间:2024-10-28 14:34:27
Wordpress html标签在手动摘录中剥离(Wordpress html tags strip on manual excerpt)

我试图制作链接,br和斜体标签适用于手动摘录但没有运气,没有摘录中断。 我已经尝试了许多不同的代码,如http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-preserve-html-tags-in-wordpress-excerpt-without-a-plugin/中提出的那样什么也没有,现在我有这个代码:

function wp_trim_all_excerpt($text) { global $post; $raw_excerpt = $text; //Add the allowed HTML tags separated by a comma $excerpt_length = apply_filters('excerpt_length', 150000); $text = wp_trim_words( $text, $excerpt_length ); //since wp3.3 $allowed_tags = '<p>,<a>,<em>,<strong>,<i>,<br>'; $text = strip_tags($text, $allowed_tags); return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); //since wp3.3 } remove_filter('get_the_excerpt', 'wp_trim_excerpt'); add_filter('get_the_excerpt', 'wp_trim_all_excerpt');

但是$ allowed_tags没有保存任何东西,我认为因为我有wp_trim,但我已经玩了几个小时,我也尝试了高级摘录插件,我在页面中激活了摘录,但是,不知道为什么,有HTML工作。

任何想法?

I have tried to make links, br and italics tags works on manual excerpt but no luck, no the excerpt breaks. I have tried many differents codes like the proposed in http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-preserve-html-tags-in-wordpress-excerpt-without-a-plugin/ and nothing, now I have this code:

function wp_trim_all_excerpt($text) { global $post; $raw_excerpt = $text; //Add the allowed HTML tags separated by a comma $excerpt_length = apply_filters('excerpt_length', 150000); $text = wp_trim_words( $text, $excerpt_length ); //since wp3.3 $allowed_tags = '<p>,<a>,<em>,<strong>,<i>,<br>'; $text = strip_tags($text, $allowed_tags); return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); //since wp3.3 } remove_filter('get_the_excerpt', 'wp_trim_excerpt'); add_filter('get_the_excerpt', 'wp_trim_all_excerpt');

but the $allowed_tags doesn't save anything, I think cause I have the wp_trim but I have played with it for hours an nothing, I also try the advanced excerpt plugin and I activated excerpt in pages, but, don't know why, there html works.

Any idea?

最满意答案

上面的解决方案仍然有一个限制,因为一些HTML标签成对出现。 如果摘录包含开始标记但不包含相应的结束标记,则网页的其余部分可能格式不正确。

此代码检查此类标记的摘录并根据需要添加结束标记:

<?php /****************************************************************************** * @Author: Richard Chonak * @Date: August 6, 2013 * @Description: Ensures closure of HTML tags opened in an automatically generated excerpt. * Also trims off any trailing incomplete HTML tag at the end of the excerpt. * @Tested: Up to WordPress version 3.6 * * @Author: Boutros AbiChedid * @Date: June 20, 2011 * @Websites: http://bacsoftwareconsulting.com/ ; http://blueoliveonline.com/ * @Description: Preserves HTML formating to the automatically generated Excerpt. * Also Code modifies the default excerpt_length and excerpt_more filters. * @Tested: Up to WordPress version 3.1.3 *******************************************************************************/ function custom_wp_trim_excerpt($text) { $raw_excerpt = $text; if ( '' == $text ) { //Retrieve the post content. $text = get_the_content(''); //Delete all shortcode tags from the content. $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]&gt;', $text); $allowed_tags = '<img>,<small>,<strong>,<em>,<b>,<i>,<p>,<br>,<a>,<blockquote>,<ul>,<li>'; /*** MODIFY THIS. Add the allowed HTML tags separated by a comma.***/ $twopart_tags = '<small>,<strong>,<em>,<b>,<i>,<p>,<br>,<a>,<blockquote>,<ul>,<li>'; /*** MODIFY THIS. Add the twopart HTML tags separated by a comma.***/ /* turn tag list into one big search pattern */ $search_patterns = "/" . str_replace(",","|",str_replace(">", "[^>]*>",$twopart_tags)) . '/'; $text = strip_tags($text, $allowed_tags); $excerpt_word_count = 200; /*** MODIFY THIS. change the excerpt word count to any integer you like.***/ $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); $excerpt_end = '[...]'; /*** MODIFY THIS. change the excerpt endind to something else.***/ $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); }; /* if fragment ends in open tag, trim off */ preg_replace ("/<[^>]*$/", "", $text); /* search for tags in excerpt */ preg_match_all ($search_patterns, $text, $matches); /* if any tags found, check for matching pairs */ $tagstack = array (""); $tagsfound = $matches[0]; while ( count ($tagsfound) > 0) { $tagmatch = array_shift($tagsfound); /* if it's a closing tag, hooray; but if it's not, then look for the closer */ if ( !strpos($tagmatch,"</") && !strpos ($tagmatch,"/>") ) { preg_match("/\pL+/",$tagmatch, $tagwords); $endtag = "</" . $tagwords[0] . ">"; /* if this tag was not closed, put the closing tag on the stack */ if (!in_array($endtag, $tagsfound) ) { array_push($tagstack,$endtag); }; }; }; /* if any unbalanced tags were found, add the closing tags */ while (count ($tagstack) > 1) { $text = $text . array_pop($tagstack); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); } remove_filter('get_the_excerpt', 'wp_trim_excerpt'); add_filter('get_the_excerpt', 'custom_wp_trim_excerpt'); ?>

The solution above still has a limitatioṅ, because some HTML tags occur in pairs. If an excerpt contains an opening tag but not the corresponding closing tag, then the rest of the web page is likely to be formatted incorrectly.

This code checks the excerpt for such tags and adds closing tags as needed:

<?php /****************************************************************************** * @Author: Richard Chonak * @Date: August 6, 2013 * @Description: Ensures closure of HTML tags opened in an automatically generated excerpt. * Also trims off any trailing incomplete HTML tag at the end of the excerpt. * @Tested: Up to WordPress version 3.6 * * @Author: Boutros AbiChedid * @Date: June 20, 2011 * @Websites: http://bacsoftwareconsulting.com/ ; http://blueoliveonline.com/ * @Description: Preserves HTML formating to the automatically generated Excerpt. * Also Code modifies the default excerpt_length and excerpt_more filters. * @Tested: Up to WordPress version 3.1.3 *******************************************************************************/ function custom_wp_trim_excerpt($text) { $raw_excerpt = $text; if ( '' == $text ) { //Retrieve the post content. $text = get_the_content(''); //Delete all shortcode tags from the content. $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]&gt;', $text); $allowed_tags = '<img>,<small>,<strong>,<em>,<b>,<i>,<p>,<br>,<a>,<blockquote>,<ul>,<li>'; /*** MODIFY THIS. Add the allowed HTML tags separated by a comma.***/ $twopart_tags = '<small>,<strong>,<em>,<b>,<i>,<p>,<br>,<a>,<blockquote>,<ul>,<li>'; /*** MODIFY THIS. Add the twopart HTML tags separated by a comma.***/ /* turn tag list into one big search pattern */ $search_patterns = "/" . str_replace(",","|",str_replace(">", "[^>]*>",$twopart_tags)) . '/'; $text = strip_tags($text, $allowed_tags); $excerpt_word_count = 200; /*** MODIFY THIS. change the excerpt word count to any integer you like.***/ $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); $excerpt_end = '[...]'; /*** MODIFY THIS. change the excerpt endind to something else.***/ $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); }; /* if fragment ends in open tag, trim off */ preg_replace ("/<[^>]*$/", "", $text); /* search for tags in excerpt */ preg_match_all ($search_patterns, $text, $matches); /* if any tags found, check for matching pairs */ $tagstack = array (""); $tagsfound = $matches[0]; while ( count ($tagsfound) > 0) { $tagmatch = array_shift($tagsfound); /* if it's a closing tag, hooray; but if it's not, then look for the closer */ if ( !strpos($tagmatch,"</") && !strpos ($tagmatch,"/>") ) { preg_match("/\pL+/",$tagmatch, $tagwords); $endtag = "</" . $tagwords[0] . ">"; /* if this tag was not closed, put the closing tag on the stack */ if (!in_array($endtag, $tagsfound) ) { array_push($tagstack,$endtag); }; }; }; /* if any unbalanced tags were found, add the closing tags */ while (count ($tagstack) > 1) { $text = $text . array_pop($tagstack); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); } remove_filter('get_the_excerpt', 'wp_trim_excerpt'); add_filter('get_the_excerpt', 'custom_wp_trim_excerpt'); ?>

更多推荐

本文发布于:2023-07-26 18:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279310.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标签   html   Wordpress   tags   excerpt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!