WordPress 要約(Excerpt)からショートコードを削除する

本文にショートコードを追加したところ、サマリ(Excerpt)にショートコードがテキストの形でそのまま公開されますね。
Shortcode in WordPress 抜粋
ショートコードを削除するには、strip_shortcodes() 関数を使用します(ここ を参照)。

例1:

<?php echo mb_strimwidth(strip_shortcodes(strip_tags(get_the_content(''))), 0, 220, '...'); ?>

例2(要約長の変更関数):

function custom_length_excerpt($word_count_limit) {
$content = wp_strip_all_tags(get_the_content() , true );
echo wp_trim_words(strip_shortcodes($content), $word_count_limit) . ' <a class="read-more" href="' . get_permalink( get_the_ID() ) . '"> Read More </a>';
}

コメントを残す

*メールアドレスは公開されません。