記事リストのページには抜粋 excerpt が表示される。
WordPress Popular posts でも記事抜粋が表示される。
そこにURLが混じっていると見にくいのでこれを除去する。
functions.php に次を追加する。
//URLの正規表現
define('URL_REG_STR', '(https?|ftp)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)');
define('URL_REG', '/'.URL_REG_STR.'/');
//本文部分の冒頭を綺麗に抜粋する echo get_content_excerpt( get_the_content(), 150 )
function get_content_excerpt($content, $length = 70){
//_v($content);
$content = preg_replace('/<!--more-->.+/is', '', $content); //moreタグ以降削除
$content = strip_tags($content);//タグの除去
$content = strip_shortcodes($content);//ショートコード削除
$content = str_replace(' ', '', $content);//特殊文字の削除
$content = preg_replace('/\[.+?\]/i', '', $content); //ショートコードを取り除く
$content = preg_replace(URL_REG, '', $content); //URLを取り除く
//$lengthが整数じゃない場合の処理
if (is_int(intval($length))) {
$length = intval($length);
} else {
$length = 70;
}
$over = intval(mb_strlen($content)) > $length;
$content = mb_substr($content, 0, $length);//文字列を指定した長さで切り取る
$content = esc_html($content);
$content = $content . '...';
return $content;
}
抜粋を表示したいところに、
<?php echo get_content_excerpt( get_the_content(), 100) ?>
と書く。 100 は抜粋の文字数で変更可能。
WordPress Popular Posts の抜粋を調整する
WordPress Popular Posts で抜粋を表示する時にURLを取り除くには、
functions.php に次を追加する。
WordPress Popular Posts では、記事のIDからexcerpt を取得しないといけない。
// wpp ID から抜粋取得 get_the_excerpt_by_id(150, 80); id=150
function get_the_excerpt_by_id($post_id='', $length=100){
global $post; $post_bu = '';
if(!$post_id){
$post_id = get_the_ID();
} else {
$post_bu = $post;
$post = get_post($post_id);
}
$mojionly = strip_tags($post->post_content);
$mojionly = strip_tags(strip_shortcodes($mojionly)); //Strips tags and images
$mojionly = str_replace(' ', '', $mojionly);//特殊文字の削除
$mojionly = preg_replace(URL_REG, '', $mojionly); //URLを取り除く
if(mb_strlen($mojionly ) > $length) $t = '...';
$content = mb_substr($mojionly, 0, $length);
$content .= $t;
if($post_bu) $post = $post_bu;
return $content;
}
// wppの出力置換
function my_custom_single_popular_post( $post_html, $popular_post, $instance ){
$archive_year = get_the_time( 'Y', $popular_post->id );
$archive_month = get_the_time( 'm', $popular_post->id );
$output = '<li><a href="' . get_permalink( $popular_post->id ). '" title="' . esc_attr( $popular_post->title ) . '">' . get_thumb_img('wpp348x208','', $popular_post->id ) . '</a><a href="' . get_permalink($popular_post->id) . '" class="wpp-post-title" title="' . esc_attr($popular_post->title) . '">' . $popular_post->title . '</a><span class="wpp-excerpt">' . get_the_excerpt_by_id($popular_post->id, 100) . '</span>';
$output .='<a href="' . get_month_link( $archive_year, $archive_month ) . '" class="wpp-date"><span class="wpp-date">' . get_the_time('Y/n/j', $popular_post->id) . '</span></a><span class="wpp-views">' . $popular_post->pageviews . '</span>';
$output .= '<span class="wpp-category-all">'. get_the_term_list( $popular_post->id, 'category','',' ','') .'</span></li>';
return $output;
}
add_filter( 'wpp_post', 'my_custom_single_popular_post', 10, 3 );
// wpp ID からアイキャッチ画像タグ取得
function get_thumb_img($size = 'wpp348x208', $alt = null, $p_id = null) {
$p_id = ($p_id) ? $p_id : get_the_ID(); //追記部分
$thumb_id = get_post_thumbnail_id($p_id);
$thumb_img = wp_get_attachment_image_src($thumb_id, $size);
$thumb_src = $thumb_img[0];
$alt = ($alt) ? $alt : get_the_title($p_id);
$thumb_img_tag = '<img src="'.$thumb_src.'" width="348" height="208" alt="'.$alt.'" title="'.$alt.'" class="lazyload wpp-thumbnail wp-post-image">';
if( empty( $thumb_src ) ){
$thumb_img_tag = '<img src="https://makotoiwasaki.com/wp-content/plugins/wordpress-popular-posts/public/images/no_thumb.jpg" class="lazyload wpp-thumbnail wp-post-image" alt="'.$alt.'" title="'.$alt.'" width="348" height="208" />';
}
return $thumb_img_tag;
}






