Разрешить 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
Вы также можете добавить текстовый редактор для отрывков,добавить ниже код в файл плагина или файл темыfunction.php,и вы сможете увидеть HTML-редактор для отрывков. Более того,он также будет отображать выдержки в формате HTML. # привет
Я скопировал это откуда-то,но не помню источник. Я использую это во всех своих проектах,и он работает.
Изменить: это скопировано из Добавление редактора форматированного текста в Excerpt 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 ); } }
Вот мой фрагмент кода.
Как разрешить HTML как
<a> <b> <i> <br>