我已经创建了一个新的自定义帖子类型,我需要由get_the_post_thumbnail()
函数设置为百分比,而不是像素,因为我使用的是流体网格。理想情况下,我只会设置宽度(为100%)。有没有办法做到这一点?可能使用add_image_size()
?
以百分比而不是像素为单位设置_POST_THMBAILE_SIZE?
3 个回复
最合适的回答,由SO网友:Reigel 整理而成
have you tried,
if(has_post_thumbnail()) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),\'full\' );
echo \'<img src="\' . $image_src[0] . \'" width="100%" />\';
}
SO网友:kaiser
无法更改默认宽度和高度输出的单位,但可以通过css覆盖输出:
/* these are the classes that get added per default */
img.wp-post-image
img.attachment-thumbnail
img.attachment-medium
img.attachment-large
img.attachment-full
然后您可以通过以下方式覆盖它body img.attachment-large { width: 50% !important; height: 50% !important; }
SO网友:Vale
我已经解决了这个问题,去掉了WP设置的所有宽度和高度属性:
//tidy up img tags. We don\'t want inline height and width added by WP.
//we\'d rather use media queries and fluid img.
function remove_image_dim_attr($html) {
$html=preg_replace( \'/width=(["\\\'])(.*?)\\1/\', \'\', $html );
$html=preg_replace( \'/height=(["\\\'])(.*?)\\1/\', \'\', $html );
return $html;
}
add_filter( \'get_image_tag\',\'remove_image_dim_attr\' );
add_filter( \'image_send_to_editor\',\'remove_image_dim_attr\' );
add_filter( \'post_thumbnail_html\',\'remove_image_dim_attr\' );
结束