允许摘录中的HTML
2 个回答
- 投票数
-
- 2017-05-29
如果需要,可以添加更多标签到
$allowed_tags = ...
function _20170529_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(']]>', ']]>', $text); $allowed_tags = '<a>,<b>,<br><i>'; $text = strip_tags($text, $allowed_tags); $excerpt_word_count = 55; /*** 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); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
Add more tags if you need into
$allowed_tags = ...
function _20170529_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(']]>', ']]>', $text); $allowed_tags = '<a>,<b>,<br><i>'; $text = strip_tags($text, $allowed_tags); $excerpt_word_count = 55; /*** 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); } } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); }
-
- 2019-04-07
您还可以添加摘录的RTF编辑器,在插件文件或主题的function.php文件中添加以下代码,您将能够看到摘录的HTML编辑器.此外,它还将以HTML格式呈现摘录. #欢呼声
我已从某处复制了此文件,但不记得源了.我在所有项目中都使用了它,并且它正在工作.
编辑:此内容是从向"摘录"中添加富文本编辑器复制而来的 2012年fuxia的答案
/** * Replaces the default excerpt editor with TinyMCE. */ add_action( 'add_meta_boxes', array ( 'T5_Richtext_Excerpt', 'switch_boxes' ) ); class T5_Richtext_Excerpt { /** * Replaces the meta boxes. * * @return void */ public static function switch_boxes() { if ( ! post_type_supports( $GLOBALS['post']->post_type, 'excerpt' ) ) { return; } remove_meta_box( 'postexcerpt', // ID '', // Screen, empty to support all post types 'normal' // Context ); add_meta_box( 'postexcerpt2', // Reusing just 'postexcerpt' doesn't work. __( 'Excerpt' ), // Title array ( __CLASS__, 'show' ), // Display function null, // Screen, we use all screens with meta boxes. 'normal', // Context 'core', // Priority ); } /** * Output for the meta box. * * @param object $post * @return void */ public static function show( $post ) { ?> <label class="screen-reader-text" for="excerpt"><?php _e( 'Excerpt' ) ?></label> <?php // We use the default name, 'excerpt', so we don’t have to care about // saving, other filters etc. wp_editor( self::unescape( $post->post_excerpt ), 'excerpt', array ( 'textarea_rows' => 15, 'media_buttons' => FALSE, 'teeny' => TRUE, 'tinymce' => TRUE ) ); } /** * The excerpt is escaped usually. This breaks the HTML editor. * * @param string $str * @return string */ public static function unescape( $str ) { return str_replace( array ( '<', '>', '"', '&', ' ', '&nbsp;' ), array ( '<', '>', '"', '&', ' ', ' ' ), $str ); } }
You can add rich text editor for excerpts as well, add below code in plugin file or theme's function.php file and you'll be able to see HTML editor for excerpts. Moreover, it'll render excerpts in HTML format as well. #cheers
I've copied this from somewhere but don't remember the source. I'm using this in my all projects and it's working.
Edit: This was copied from Adding a rich text editor to Excerpt 2012 answer by fuxia
/** * Replaces the default excerpt editor with TinyMCE. */ add_action( 'add_meta_boxes', array ( 'T5_Richtext_Excerpt', 'switch_boxes' ) ); class T5_Richtext_Excerpt { /** * Replaces the meta boxes. * * @return void */ public static function switch_boxes() { if ( ! post_type_supports( $GLOBALS['post']->post_type, 'excerpt' ) ) { return; } remove_meta_box( 'postexcerpt', // ID '', // Screen, empty to support all post types 'normal' // Context ); add_meta_box( 'postexcerpt2', // Reusing just 'postexcerpt' doesn't work. __( 'Excerpt' ), // Title array ( __CLASS__, 'show' ), // Display function null, // Screen, we use all screens with meta boxes. 'normal', // Context 'core', // Priority ); } /** * Output for the meta box. * * @param object $post * @return void */ public static function show( $post ) { ?> <label class="screen-reader-text" for="excerpt"><?php _e( 'Excerpt' ) ?></label> <?php // We use the default name, 'excerpt', so we don’t have to care about // saving, other filters etc. wp_editor( self::unescape( $post->post_excerpt ), 'excerpt', array ( 'textarea_rows' => 15, 'media_buttons' => FALSE, 'teeny' => TRUE, 'tinymce' => TRUE ) ); } /** * The excerpt is escaped usually. This breaks the HTML editor. * * @param string $str * @return string */ public static function unescape( $str ) { return str_replace( array ( '<', '>', '"', '&', ' ', '&nbsp;' ), array ( '<', '>', '"', '&', ' ', ' ' ), $str ); } }
这是我的摘录代码.
如何允许类似
<a> <b> <i> <br>