我试图在这段代码中控制随机图像的输出。最终目标是不输出width
和height
图像的属性。
我想要这个:
<img src="URL HERE" alt="ALT HERE"/>
但我不想这样(这是下面的代码当前输出的):
<img src="URL HERE" alt="ALT HERE" width="200px" height="200px" />
My code:
$thumb_id = get_post_thumbnail_id(get_the_ID());
$args = array(
\'order\' => \'ASC\',
\'orderby\' => \'rand\',
\'post_type\' => \'attachment\',
\'post_parent\' => $post->ID,
\'post_mime_type\' => \'image\',
\'post_status\' => null,
\'numberposts\' => 1,
\'exclude\' => $thumb_id
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_image($attachment->ID, \'full\', false);
}
}
最合适的回答,由SO网友:fuxia 整理而成
使用wp_get_attachment_image_src()
并构建一个简化的img
要素您可以使用类似以下代码的内容来替换wp_get_attachment_image()
:
function wpse_53524_get_image_tag( $attach_id, $size, $icon, $alt = \'\' )
{
if ( $src = wp_get_attachment_image_src( $attach_id, $size, $icon ) )
{
return "<img src=\'{$src[0]}\' alt=\'$alt\' />";
}
}
将上述功能放入主题
functions.php
. 像这样称呼它
wp_get_attachment_image()
:
foreach ( $attachments as $attachment ) {
echo wpse_53524_get_image_tag( $attachment->ID, \'full\', false );
}