WordPress Development Asked on November 9, 2021
So I’m making a website divived in two parts, one is for the text, and the other part is for the images.
What I’m trying to do, is to display the specific images belonging to each post.
I’m also using Advanced Custom Fields to make it easier for me.
So I know can have my id post using:
global $post; echo $post->ID;
What I don’t know, is how to display the images corresponding to that.
here is my code:
<?php
$images = get_field('gallery');
if( $images ): ?>
<ul> <div class="slide-photos" id="<?php $post->ID;?>" data-id="<?php $post->ID;?>">
<?php foreach( $images as $image ): ?>
<li>
<div class="photos-ind"><img id="<?php $post->ID;?>" class="slide-photos" src="<?php echo esc_url($image['sizes']['thumbnail']); ?>" data-id="<?php $post->ID;?>"/></div>
</li>
<?php endforeach; ?></div>
</ul>
<?php endif; ?>
I also have that javascript
$(document).ready(function(){
$(".exposition").on('click',function(){
var hello = $(this).attr('data-id');
$('.slide-photos').hide();
$('[id='+ hello + ']').show();
$('[id='+ hello + ']').flickity({
wrapAround: true, lazyLoad: 2, selectedAttraction: 0.1,
friction: 0.5, cellAlign: 'left', adaptiveHeight: false, imagesLoaded: true,
});
console.log('[id='+ hello + ']');
});
});
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:
Answered by Roel Magdaleno on November 9, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP