簡體   English   中英

獲取圖片自定義帖子圖片大小-Wordpress

[英]Get image custom post image size - Wordpress

我有以下代碼:

<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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM