【问题标题】:Get image source by ID with size通过 ID 和 size 获取图像源
【发布时间】:2013-06-24 20:26:29
【问题描述】:

我正在开发一个 WP 网站,其中包含一些带有 Google Analytics(分析)点击事件的促销/广告滑块。效果很好,现在我想以正确的分辨率提供正确的图像。

我使用picturefill 来提供图像。硬编码时工作正常,所以我知道它有效。当我尝试使用 WP 上传的图像获取图像源时,没有。我知道这是因为我的(缺乏)php 技能。

图片填充需要什么:

<span data-picture data-alt="">
    <span data-src="filename_default.jpg"></span>
    <span data-src="filename_small.jpg" data-media="(min-width: 400px)"></span>
    <span data-src="filename_medium.jpg" data-media="(min-width: 768px)"></span>
    <span data-src="filename_big.jpg" data-media="(min-width: 1200px)"></span>

    <!-- Fallback content for non-JS browsers. -->
    <noscript>
        <img src="external/imgs/small.jpg" alt="">
    </noscript>
</span>

我将 url 或 id 或图像存储在: $attachment_id

我想这样做:

<?php
    $attachment_id = get_field('advimg');
    $large = "adv-pos-a-large";
    $default = "adv-pos-a-default";
    $small = "adv-pos-a-small";

?>

get_field('advimg'); 来自ACF

<span data-picture data-alt="">
    <span data-src="<?php wp_get_attachment_image_src( $attachment_id, $default ); ?>"></span>
    <span data-src="<?php wp_get_attachment_image_src( $attachment_id, $small ); ?>" data-media="(min-width: 400px)"></span>
    <span data-src="<?php wp_get_attachment_image_src( $attachment_id, $default ); ?>" data-media="(min-width: 768px)"></span>
    <span data-src="<?php wp_get_attachment_image_src( $attachment_id, $large ); ?>" data-media="(min-width: 1200px)"></span>

    <!-- Fallback content for non-JS browsers. -->
    <noscript>
        <img src="external/imgs/small.jpg" alt="">
    </noscript>
</span>

但它不起作用。我玩过:

wp_get_attachment_image_src wp_get_attachment_image_url wp_get_attachment_image_link

我一定是在度过星期五,因为它不工作,而且有些东西对我说这并不难……我只是今天没看到。

希望你们能参与进来。

提前致谢,

/保罗

编辑/最终代码/修复

<?php 
    $attachment_id = get_field('advanced_custom_field_name_here');
    $small = wp_get_attachment_image_src( $attachment_id, 'adv-pos-a-small' );
    $default = wp_get_attachment_image_src( $attachment_id, 'adv-pos-a-default' );
    $large = wp_get_attachment_image_src( $attachment_id, 'adv-pos-a-large' );
?> 

<span data-picture data-alt="alt desc here">
    <span data-src="<?php echo $default[0]; ?>"></span>
    <span data-src="<?php echo $small[0]; ?>" data-media="(min-width: 400px)"></span>
    <span data-src="<?php echo $default[0]; ?>" data-media="(min-width: 768px)"></span>
    <span data-src="<?php echo $large[0]; ?>" data-media="(min-width: 1200px)"></span>

    <!-- Fallback content for non-JS browsers. Same img src as the initial, unqualified source element. -->
    <noscript>
        <img src="<?php echo $default[0]; ?>" alt="alt desc here">
    </noscript>
</span>

【问题讨论】:

    标签: wordpress responsive-design pageload picturefill


    【解决方案1】:

    wp_get_attachment_image_src 的名称令人困惑。 (更好的名称是 wp_get_attachment_image_atts)。它实际上返回一个数组,其中包含图像附件文件的图像属性“url”、“width”和“height”。对于图像 src,使用返回数组中的第一个元素:

    $img_atts = wp_get_attachment_image_src( $attachment_id, $default );
    $img_src = $img_atts[0];
    

    另外,请确保您的 ACF 图像字段返回类型设置为附件 ID,以便 $attachment_id = get_field('advimg'); 为您提供您期望的 ID。

    【讨论】:

    • 谢谢! +1以获得详细答案。我会在这个星期一检查,会通知你!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多