1
votes

I have build the custom post template using acf plugin for portfolio website There are 3~15 images on one image gallery and the number of images each other on every post page. And I want to get image galleries url on page template I have already get the post id of each post page and want to get image galleries url from post id

1
Have you tried get_field('field_name', 22);? 22 being the ID of the post. If that isn't what you need, then you will need to show an example of the code you have tried.WizardCoder
Yes, right. I need it But on my code , get_field('field_name', post_ID); not working Instead of this, if I echo get_sub_field('field_name', post_ID), it show the lastest post id's image galleriesXuanmin Zhu

1 Answers

1
votes

I'd recommend taking a look at ACF's documentation, which has excellent code examples for all of the fields.

https://www.advancedcustomfields.com/resources

To get image URLs from the post ID, you can use get_field() to get the entire array of images, and then loop through them to get the 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>';

Each image is itself an array with everything you need:

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
  )
)