Autoriser HTML dans l'extrait
2 réponses
- votes
-
- 2017-05-29
Ajoutez d'autresbalises sinécessaire dans
$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
Vouspouvez également ajouter un éditeur detexte richepour lesextraits,ajouter le code ci-dessous dans lefichierplugin ou lefichierfunction.php duthèmeet vouspourrez voir l'éditeur HTMLpour desextraits. Deplus,il rendra également desextraits auformat HTML. #cheers
J'ai copié ceci de quelquepartmaisje neme souvienspas de la source. J'utilise ça danstousmesprojetset çamarche.
Modifier: ceci a été copié àpartir de Ajout d'un éditeur detexteenrichi à l'extrait Réponse 2012 defuxia
/** * 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 ); } }
Voicimonextrait de code.
Commentpuis-je autoriser le HTML comme
<a> <b> <i> <br>