如何在WordPress中从帖子页面ID获取ACF的图库图像URL



我已经使用for Portfolio网站构建了自定义邮政模板一个图像库上有3〜15张图像,并且每个帖子页面上都有图像数量。我想在页面模板上获取图像库URL我已经获得了每个帖子页面的帖子ID,并希望从post ID

获取图像库URL

我建议您查看ACF的文档,该文档在所有字段中都有出色的代码示例。

https://www.advancedcustomfields.com/Resources

要从帖子ID中获取图像URL,您可以使用get_field()获取整个图像,然后循环通过它们获取URL。

// get the array of all gallery images from the current post
$images = get_field( 'gallery' );
// get the array of all gallery images from the given post id
$post_id = 17;
$images = get_field( 'gallery', $post_id );
// example markup: create an unordered list of images
echo '<ul>';
// loop through the images to get the URL
foreach( $images as $image ) {
  // the url for the full image
  $image_url = $image['url'];
  // the url for a specific image size - in this case: thumbnail
  $image_thumbnail_url = $image['sizes']['thumbnail'];
  // render your markup inside this loop, example: an unordered list of images
  echo '<li>';
  echo '<img src="' . $image_url . '">';
  echo '</li>';
}
echo '</ul>';

每个图像本身都是一个带有所需的一切的数组:

Array (
  [ID] => 2822
  [alt] => 
  [title] => Hot-Air-Balloons-2560x1600
  [caption] => 
  [description] => 
  [mime_type] => image/jpeg
  [type] => image
  [url] => http://acf5/wp-content/uploads/2014/06/Hot-Air-Balloons-2560x1600.jpg
  [width] => 2560
  [height] => 1600
  [sizes] => Array (
    [thumbnail] => http://acf5/wp-content/uploads/2014/06/Hot-Air-Balloons-2560x1600-150x150.jpg
    [thumbnail-width] => 150
    [thumbnail-height] => 150
    [medium] => http://acf5/wp-content/uploads/2014/06/Hot-Air-Balloons-2560x1600-300x187.jpg
    [medium-width] => 300
    [medium-height] => 187
    [large] => http://acf5/wp-content/uploads/2014/06/Hot-Air-Balloons-2560x1600-1024x640.jpg
    [large-width] => 604
    [large-height] => 377
    [post-thumbnail] => http://acf5/wp-content/uploads/2014/06/Hot-Air-Balloons-2560x1600-604x270.jpg
    [post-thumbnail-width] => 604
    [post-thumbnail-height] => 270
  )
)

最新更新