【问题标题】:Advanced Custom Fields wordpress image size高级自定义字段 wordpress 图像大小
【发布时间】:2013-10-06 01:43:15
【问题描述】:

我正在创建一个带有轮播的网站。为了加载图片,我在 Wordpress 上使用了高级自定义字段。

这是我的代码:

<?php $images = get_field('slides', $post->ID);
// clean_print_r($images);
if (!empty($images)) :
?>
<div class="wide-container">
    <div id="slides">
        <ul class="slides-container">
        <?php for($i = 0; $i < count($images); $i++): ?>
        <!-- slides -->
            <li>
                <img src="<?php echo $images[$i]['img_slide']['sizes']['large'] ?>" alt="" />
            </li>
        <?php endfor; ?>
        </ul>
    </div>
</div>
<?php endif; ?>

我可以加载图片,但它们的大小为 1024 像素宽:

<img src="http://example.com/wp-content/uploads/2013/09/bg_header03-1024x341.jpg" ... />

有没有办法获得全尺寸的图像?我试图替换:

['img_slide']['sizes']['large']

['img_slide']['sizes']['full']

但这不起作用,并且没有加载任何图像。 在 ACF 中,我通过 ID 调用图像附件,它是一个转发器字段。

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    我不确定如何使用返回 ID,但如果你返回 URL,你会得到完整的图像。

    编辑: 好的,我用 Image ID 做了一些测试,看起来你已经以某种方式混淆了你的数组处理。这适用于单个图像:不过应该很容易适应您的中继器。

    $attachment_id = get_field('slide');
    $size = "full";
    
    
    $image = wp_get_attachment_image_src( $attachment_id, $size );
    echo '<img src="' . $image[0] . '">';   
    
    //OR
    
    $image = wp_get_attachment_image( $attachment_id, $size );
    echo $image[0]; 
    

    【讨论】:

      【解决方案2】:

      我之前的回答是关于 return: image ID,由线程启动器指定,但现在我意识到他实际上是在谈论 return: object。

      /*
      *  Return value = Object
      *  requires ACF 3.3.7+
      */
      
      $image = get_field('image');
      
      var_dump($image);
      
      /*
      
      Data returned will look like this:
      
      Array
      (
          [id] => 540
          [alt] => A Movie
          [title] => Movie Poster: UP
          [caption] => sweet image
          [description] => a man and a baloon
          [url] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
          [sizes] => Array
              (
                  [thumbnail] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-150x150.jpg
                  [medium] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-300x119.jpg
                  [large] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
                  [post-thumbnail] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
                  [large-feature] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
                  [small-feature] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-500x199.jpg
              )
      
      )
      
      */
      

      来源:http://www.advancedcustomfields.com/resources/field-types/image/

      显然原始图像不是大小而是 URL,因此更改:

      ['img_slide']['sizes']['large']
      

      ['img_slide']['url']
      

      你应该没事的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-06
        • 2019-11-07
        • 1970-01-01
        • 2012-09-02
        • 2017-08-28
        • 2012-08-10
        • 2015-01-21
        • 2017-05-01
        相关资源
        最近更新 更多