2013年3月20日水曜日

[WP]記事に挿入する画像のwidth、heightのデフォルト表記無効化

custom fieldで指定した画像を読み込むときに、
デフォルトで入るwidthとheightを外す方法。

正当にはfunctions.phpで記入したもので動くはずなのですが、

何故か動かなかったので、ちょっと強引にstyle直書きで効果を上書きしました。
wp_get_attachment_image(get_post_meta($post->ID, 'postcft_photo', true), 'post-thumbnail', false, array( 'alt'=>the_title( '' , '' , false ) . 'の写真', 'title'=>the_title( '' , '' , false ) . 'の写真', 'style'=>'height:auto;' ) )
ちなみに何故か効かなかったfunctions.phpへの記述は以下。
多分何かがおかしいのだけど、何がおかしいか分からないので、また今度考えよう。

add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );

function remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}

0 件のコメント:

コメントを投稿