WordPress 要約では、特定のHTMLタグを維持させる方法

Last Updated:2015年10月01日| | コメントを残す

WordPress 要約(Excerpt)は、特定のタグを維持させようとする場合には、例えば、 、 、タグなどの特定のHTMLタグを使用する場合はstrip_tags()関数を使用して、特定のタグを活性化させる方法を考えてみることができます。 strip_tags() 関数は、次のように使用されます。

strip_tags($contant,'허용할 태그');

例えば、次のようなコードを使用すると、ショートコードとタグ(許可リストのタグを除く)が削除されます。

$content = get_the_content('');
$content = strip_shortcodes( $content );
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$allowed_tags = '<p>,<a>,<em>,<strong>';
$content = strip_tags($content, $allowed_tags);

最終的に次の関数を使用することができます。

function custom_wp_trim_excerpt($text) {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');

$text = strip_shortcodes( $text );

$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]&gt;', $text);

/***Add the allowed HTML tags separated by a comma.***/
/***허용할 HTML 태그를 콤마로 분리하는 형식으로 추가***/
$allowed_tags = '<p>,<a>,<em>,<strong>'; /*허용할 태그 목록 */
$text = strip_tags($text, $allowed_tags);

/***Change the excerpt word count.***/
/***요약문 단어수 변경***/
$excerpt_word_count = 60;
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);

/*** Change the excerpt ending.***/
/*** 요약문 끝부분 변경***/
$excerpt_end = ' <a href="'. get_permalink($post->ID) . '">' . '&raquo; Continue Reading.' . '</a>';
$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);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');

この関数の詳細説明は、 ここを参照してください。 上記の関数を WordPress テーマフォルダ内のfunctions.phpファイルに追加するだけです。 直接テストはしていなかったが、この関数を使用して基本的な概念を理解することができます。 (ハングル要約では、いくつかのハングルが文字化けすることができます。)

 


コメントを残す

コメント