문제

When I use Advanced Custom Fields I can return the full image array of an upload. Is there a native WordPress way to get the full image array for any image in the Media Library by image ID? I can find ways to get the alt text and a specific image size, but not the array itself...

도움이 되었습니까?

해결책

The "full array" is constructed by Advanced Custom Fields from various sources. It's not a format that occurs natively in WordPress.

If you want to output an image tag for an image with all the correct attributes, just use the ID with wp_get_attachment_image(). That returns an HTML <img> tag with the src, width, height, alt, srcset, sizes and class all populated. You can add or change attributes to the tag using the 4th argument:

echo wp_get_attachment_image(
    $attachment_id,
    'large',
    false
    [
        'class'          => 'custom-class',
        'data-attribute' => 'custom-attribute-value',
    ]
);

To be honest, if your goal is to ultimately get an <img> tag, then even with ACF you should return an ID and use this function.

다른 팁

If you're using Autoload/Composer you can use $image = \acf_get_attachment( $attachment_id ); to get an acf image array. If not I guess you can use require_once() to load your plugins/advanced-custom-fields-pro/includes/api/api-helpers.php, however I haven't tested this yet.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 wordpress.stackexchange
scroll top