LoginSignup
2
2

More than 5 years have passed since last update.

WordPressのアイキャッチ出力からwidth,height属性を排除する方法

Last updated at Posted at 2014-01-22

RSSにアイキャッチの画像を追加して他のサイトでそのRSS情報を表示したい。
で、その時アイキャッチ画像をそのままthe_post_thumbnail()で出力するとwidth,height属性が自動でついちゃうのでRSS情報を表示する先でサイズ変更の自由度が下がる。
なので、RSSに出力する際に正規表現でwidthとheightを削除して渡すようにする。

<?php $html = get_the_post_thumbnail(); ?>
<thumb_img><![CDATA[<?php echo preg_replace('/(width|height)=\"\d*\"\s/', "", $html); ?>]]></thumb_img>
?>

<thumb_img>というエレメントをRSSに足してそこにアイキャッチ画像エレメントを追加。
この時、CDATAで文字列扱いにすること。PHPのsimplexml_load_file()で取得する時に、XMLのエレメントと思われちゃうので。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2