我有以下代码:
<div class="content project clearfix">
<?php
if( have_rows('galeria') ):
while ( have_rows('galeria') ) : the_row(); ?>
<img class="project-item" src="<?php the_sub_field('imagen'); ?>">
<?php endwhile;
endif;
?>
</div>
问题是每个img
("imagen")都有不同的大小,我得到的输出是:
<img class="project-item" src="http://test.local/wp-content/uploads/2014/11/project-5.jpg">
正如您所看到的,没有设置宽度或高度。如何获取attr上图像的宽度和高度?
您可以使用类似getimagesize()
函数的东西-我认为这是一个gdlib
函数,它通常与任何PHP安装一起安装:
$fileInfo = getimagesize(the_sub_field('imagen'));
这将产生一个类似的阵列:
Array
(
[0] => 800
[1] => 799
[2] => 2
[3] => width="800" height="799"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
使用类似:
<?php
$img = the_sub_field('imagen');
$fileInfo = getimagesize($img); ?>
<img class="project-item" src="<?php echo $img; ?>" <?php echo $fileInfo[3]; ?> />
应产生:
<img class="project-item" src="http://test.local/wp-content/uploads/2014/11/project-5.jpg" width="800" height="799" />