ページや記事の一覧表示をする時に、
文章文章文章文章[ショートコード] 文章…
を
文章文章文章文章文章…
にする。
テーマのfunctions.phpなどに以下と追加
function remove_shortcode ($a) {
// パラメータ$aは表示する文字数
if(has_excerpt()) {
$base_content = strip_shortcodes(get_the_excerpt());
$base_content = str_replace(array("\r\n", "\r", "\n"), "", $base_content);
$trim_content = mb_substr($base_content, 0, $a ,"utf-8");
} else {
$base_content = strip_shortcodes(get_the_content());
$base_content = preg_replace('!<style.*?>.*?</style.*?>!is', '', $base_content);
$base_content = preg_replace('!<script.*?>.*?</script.*?>!is', '', $base_content);
$base_content = strip_tags($base_content);
$trim_content = mb_substr($base_content, 0, $a ,"utf-8");
$trim_content = mb_ereg_replace(' ', '', $trim_content);
};
echo $trim_content . '…';
}
使用例
archive.phpなど一覧を表示するテンプレートのループに
<?php if ( have_posts() ) : ?>;
<p><a href="<?php the_permalink(); ?>">
<?php
if (has_excerpt()) {
the_excerpt();
} else {
function_exists('remove_shortcode')) {
remove_shortcode(150);
}else{
the_excerpt();
}
}
?></a></p>
<?php endif; ?>
本サイトのトップページでも使用しています。

コメントを残す
コメントを投稿するにはログインしてください。