繁体   English   中英

如何从wordpress中的帖子页面ID获取acf的图库图片网址

[英]How to get acf's gallery images url from post page id in wordpress

我已经使用 acf 插件为投资组合网站构建了自定义帖子模板 一个图片库上有 3~15 张图片,每个帖子页面上的图片数量各不相同。 我想在页面模板上获取图片库 url 我已经获得了每个帖子页面的帖子 ID,并希望从帖子 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
  )
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM