As far as I know get_field() function will return the response according to the current post (There’s no need to specify the post ID). But if you want to specify the post ID then you have to type it in the second argument, like:
get_field( $selector, $post_id );
So your code would be like this:
<?php
global $post;
$post_id = 0 !== $post->ID ? $post->ID : false;
$images = get_field( 'gallery', $post_id );
if ( $images ): ?>
// Your content.
<?php endif; ?>
Do not use $post->ID directly in the second argument because the value might be 0 sometimes if WP_Query fails. If that happens use false to target the current post.
Check the resources:
Related Posts:
- How do I change/modify the_post_thumbnail(); html output?
- How to add a class to the attachment images
- Change the filename format of saved featured images
- How to register images uploaded via FTP in media library?
- Using file_exists to check file in Uploads
- Rename image uploads replacing “width” and “height” in filename with “medium”, “large”, “small”
- Rename image uploads with width in filename
- Admin Media grid view images won’t load
- WordPress remove EXIF Data from specific Thumb
- Add custom image sizes to media uploader
- Conditional Statement – check if post has an attachment image
- How to upload imagick resource to media in wordpress
- Use Filename for Alt and Title Tags
- Change Width of Featured Image Thumbnail on Add/Edit Post Page
- Use Timber/Twig to pull an image by image ID [closed]
- Adding graphics to 404.php page
- Add custom HTML to posts page
- Strip Image Classes from HTML Output
- Additional image sizes are not being generated
- Adding a querystring to an image URL when clicking ‘insert into post’?
- How to insert a local image with custom size
- Change logo based on incoming domain
- Theme currently showing thumbnails, how to show full size?
- Validate Uploaded Image using WordPress’ Built-in Functions?
- Get most recent media upload
- Convert all images to PNG on file upload
- Auto crop images in WordPress
- How do I let users upload files to a chosen location?
- Get different images for mobile and desktop with php, advanced custom fields without using js and ajax
- add_image_size for header_image
- Cannot display or echo alt text on featured image
- Recent posts with featured image or fallback image with permalink
- Set the background to a default image if there isn’t a specified “featured image”
- Mass update excerpt
- How to control WordPress image metadata (using Imagick)?
- Reprinting tags with all attributes
- Can’t upload image using media_sideload_image
- How to use thumbnail size of image if I’m only using src to get image
- What is the alternative to “ when it comes to calling Media (image) files in the ‘attachment.php’ file?
- Accessing an image folder inside public web directory
- Why is image hard cropping for one image using add_image_size not working?
- Enable custom logo upload if logo is not in header
- Scrape external webpage for first image and add it to new post
- “imagejpeg” function is not working in wordpress
- WordPress function when file is uploaded, deleted or edited
- Download images from media library to computer in BULK?
- Three Questions with Twenty Eleven theme
- How to loop through all the attached images in a post, and get their url one by one
- Which function crops images in wordpress?
- Division by zero error in image.php
- Resize image to specific dimension (X to Y ratio)
- How can I automatically resize up images to fit into all of my registered image sizes
- Clickable image link sends people to wrong URL
- Only show image from custom field when present
- Upload non-featured image to image field
- I’m unable to call img path using single quotes in an array?
- Get the id of all images in a post
- Display an image based on field value
- Else/If Statement to Display Photo Descriptions
- Controlling image output size ACF repeater [closed]
- Image rotation issue (horizontal picture uploading as vertical) — Exif issue?
- WordPress İmages Upload & Delete Error
- Generate responsive post thumbnails of same size
- Attaching images to posts manually (i.e. with custom code, without the use of an API)
- Adding a ‘style=’ bit to image_send_to_editor output
- How to internationalize header image?
- Generate images with html2canvas and the woocommerce api
- WordPress api post image raw data without being blank in media library
- Retrieve alt text by referencing the img src
- Show next image in array if page is revisited by same logged user
- WordPress loading all local images from specific directory?
- not outputting medium size image URL
- I have a background-image in css file but don’t show in site.how to fix this prblm. i sent screenshot. thx
- How do I stop an Image Address from loading in the Browser?
- How to add a PHP scripts into WordPress
- How to include different html tags in the header for different pages?
- How do I get the featured post to share on Social Media and show on Individual Post pages?
- add class to background image
- Extend the WP_Customize_Image_Control class to change its non-frame $button_labels
- Save the outputted image into the Media Library, with a different filename and extension
- How to resize an image server-side before upload?
- Creating image gallery in wordpress
- Image limit from 1 to 5
- Gallery requires a featured image but does not show it in posts/pages
- Snapshot Theme from WooThemes — empty ‘alt’ attribute
- Changing 2011 theme header image based on user dropdown
- Setting post thumbnail sizes in template file and functions.php
- Determine if only one image attached to a post/page
- upload images on front by user using form
- PHP-Code inside Href-Tag
- HTML in PHP problem [closed]
- Adding slides to an existing carousel manually
- An automatic way to crop images of different sizes
- How to automatically convert images to WebP on WordPress?
- Remove default Image Sizes
- Understanding responsive imagery
- Use URL query string in img src attribute
- Add/alter image attribute before render
- Restricting Image Upload Sizes using ‘wp_handle_upload_prefilter’ – Stuck media progress bar when Featured Image?
- Image alt tag is showing post title instead of Alt Text