발췌문에서 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 형식으로 렌더링합니다. # 건배
어딘가에서 복사했지만 출처가 기억 나지 않습니다. 나는 내 모든 프로젝트에서 이것을 사용하고 있으며 작동하고 있습니다.
편집 : 서식있는 텍스트 편집기를 발췌에 추가 fuxia의 2012 년 답변
<사전> <코드> /** * 기본 발췌 편집기를 TinyMCE로 대체합니다. */ add_action ( 'add_meta_boxes',array ( 'T5_Richtext_Excerpt','switch_boxes')); 클래스 T5_Richtext_Excerpt { /** * 메타 박스를 대체합니다. * * @return 무효 */ 공용 정적 함수 switch_boxes () { if (!post_type_supports ($ GLOBALS [ 'post']->post_type,'excerpt')) { 반환; } remove_meta_box ( 'postexcerpt',//ID '',//화면,모든 게시물 유형을 지원하기 위해 비어 있음 'normal'//컨텍스트 ); add_meta_box ( 'postexcerpt2',//'postexcerpt'만 재사용하는 것은 작동하지 않습니다. __ ( 'Excerpt'),//제목 array (__CLASS__,'show'),//표시 기능 null,//Screen,메타 박스가있는 모든 화면을 사용합니다. 'normal',//컨텍스트 'core',//우선 순위 ); } /** * 메타 박스 출력. * * @param 객체 $post * @return 무효 */ 공개 정적 함수 show ($post) { ? > & lt; label class="screen-reader-text"for="excerpt"> & lt;?php _e ( '발췌') ? > & lt;/라벨 > & lt;?php //기본 이름 인 'excerpt'를 사용하므로 신경 쓸 필요가 없습니다. //저장,기타 필터 등 wp_editor ( self :: unescape ($post- >post_excerpt), '발췌', 배열 ( 'textarea_rows'=> 15, 'media_buttons'=> 그릇된, 'teeny'=> 진실, 'tinymce'=> 진실 ) ); } /** * 발췌는 일반적으로 이스케이프됩니다. HTML 편집기가 중단됩니다. * * @param 문자열 $ str * @return 문자열 */ 공용 정적 함수 unescape ($ str) { 반환 str_replace ( 배열 ( '& amp; lt;','& amp;gt;','& amp; quot;','& amp; amp;','& amp;nbsp;','& amp; amp;nbsp;'), 배열 ( '& lt;','>',' "','& amp;','',''), $ 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>