アイキャッチ画像のaltやclassやwidthやheight
レスポンシブでコーディングする時ってWordpressのアイキャッチ画像に「width」「hight」が入っているのが邪魔です。 オリジナルのテーマデコーディングしていると「class」も不要な場合が多く、代変えテキスト未入力の「alt」も ファイル名では無く投稿タイトルに変更したい場合の忘備録です。
functions.phpに以下を記述します。
1 2 3 4 5 6 7 8 9 |
add_filter( 'post_thumbnail_html', 'remove_thumbnail_html'); function remove_thumbnail_html( $html, $post_id, $post_image_id ) { $title= get_the_title(); $html = preg_replace('/ width="\d+"/', '', $html); $html = preg_replace('/ height="\d+"/', '', $html); $html = preg_replace('/ alt=".*\w+"/', ' alt="'.$title.'"', $html); $html = preg_replace('/ class=".*\w+"/', '', $html); return $html; } |
classを先に消しちゃうと代変テキスト未入力のaltも消えちゃったので、この順で!
- 2015年03月02日月曜日
- :Naruhiko Wakai
comments