の画像タグに alt と title 属性を追加する。
WordPress Popular Posts のサムネイル画像に alt と title を追加するには functions.php に次の様に追記する。
//wppサムネイル取得先変更 alt title 追加
function change_wpp_thumb( $content, $p ) {
$thumb_html = get_the_post_thumbnail( $p->id, 'wpp348x208' );
preg_match( '!src="([^"]+)"!', $thumb_html, $thumb );
if (has_post_thumbnail($p->id)){//投稿にサムネイルがある場合の処理
$new_content = preg_replace('!<img(.+?)? src="[^"]+"(.+?)>!', '<img$1 src="' . $thumb['1'] . '"$2 alt="' . esc_attr($p->title) . '" title="' . esc_attr($p->title) . '" >', $content );
} else {//サムネイルなしの場合の処理
$new_content = preg_replace('!<img(.+?)? src="[^"]+"(.+?)>!', '<img$1 src="https://makotoiwasaki.com/wp-content/themes/jstork_custom/no_thumb.jpg"$2 alt="' . esc_attr($p->title) . '" title="' . esc_attr($p->title) . '" >', $content );
}
return $new_content;
}
if ( !is_admin() ) add_filter( 'wpp_post', 'change_wpp_thumb', 10, 2 );//管理画面では除外
同じフック wpp_post で暦年も置換変更する場合。
//wppサムネイル取得先変更 暦変更
function change_wpp_thumb( $content, $p ) {
$thumb_html = get_the_post_thumbnail( $p->id, 'wpp348x208' );
preg_match( '!src="([^"]+)"!', $thumb_html, $thumb );
if (has_post_thumbnail($p->id)){//投稿にサムネイルがある場合の処理
$new_content = preg_replace('!<img(.+?)? src="[^"]+"(.+?)>!', '<img$1 src="' . $thumb['1'] . '" width="348" height="208" class="lazyload wpp-thumbnail wpp_def_no_src wpp_custom_field" alt="' . esc_attr($p->title) . '" title="' . esc_attr($p->title) . '" >', $content );
} else {//サムネイルなしの場合の処理
$new_content = preg_replace('!<img(.+?)? src="[^"]+"(.+?)>!', '<img$1 src="https://makotoiwasaki.com/wp-content/themes/jstork/library/images/noimg.png" width="348" height="208" class="lazyload wpp-thumbnail wpp_def_no_src wpp_custom_field" alt="' . esc_attr($p->title) . '" title="' . esc_attr($p->title) . '" >', $content );
}
$new_content=str_replace("2019.", "2679.", $new_content);
$new_content=str_replace("2018.", "2678.", $new_content);
$new_content=str_replace("2017.", "2677.", $new_content);
$new_content=str_replace("2016.", "2676.", $new_content);
$new_content=str_replace("2015.", "2675.", $new_content);
$new_content=str_replace("2014.", "2674.", $new_content);
return $new_content;
}
if ( !is_admin() ) add_filter( 'wpp_post', 'change_wpp_thumb', 10, 2 );//管理画面では除外




