Use image ACF with get_previous_post - php

In an article I want to have the link of the previous article, including the title of the article and a ACF custom image.
I now how to make the link of the previous title project :
<?php
$prev_post = get_previous_post();
if (!empty( $prev_post )): ?>
<a href="<?php echo $prev_post->guid ?>">
<?php echo $prev_post->post_title ?>
</a>
<?php endif ?>
Here's my ACF code for the current image :
<?php
$image = get_field('image_cover');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
But how to do the same thing (get_previous_link) for my ACF image ?

You can use
$image = get_field('image_cover', $prev_post->ID);
This will get the custom field image_cover from the previous post using the ID supplied by the previous post object.

Related

Adding links within ACF Image Caption

So I'm using ACF Image: https://www.advancedcustomfields.com/resources/image/, and outputting my image and caption as an Array.
I'd like to be able to add links within my captions e.g. '', but when I do this it literally outputs the code rather than turning the text into a link.
Is there a way to make it be a link rather than just outputting the HTML as text.
This is my code for my image:
<?php
$image = get_sub_field('image'); if( $image ):
// Image variables.
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
$caption = $image['caption'];
?>
<img class="img-fluid" src="<?php echo esc_url($url); ?>" alt="<?php echo esc_attr($alt); ?>" />
<?php if( $caption ): ?>
<p class="caption"><?php echo esc_html($caption); ?></p>
<?php endif; ?>
<?php endif; ?>
But when I add this to the caption box within Wordpress: This image is from Flickr.
It literally outputs the same as above rather than turning the word into a link.
As you can see above I had the code:
<?php echo esc_html($caption); ?>
When I removed the 'esc_html' part then I was able to add links within the caption area. So the code for the Caption was this instead:
<?php if( $caption ): ?>
<p class="caption"><?php echo ($caption); ?></p>
<?php endif; ?>

wp_query for an Advanced Custom Fields gallery

I am trying to make a load more button that will load more images from my ACF Gallery using AJAX. But to do this I need to be able to use wp_query so that I can set the amount of posts per page. So is it possible to convert the following code into a wp_query?
<?php
$images = get_field('images');
if( $images ): ?>
<?php foreach( $images as $image ): ?>
<div class="col-4 p-0">
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['gallery_size']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
Have you looked at: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
Basically you can query ACF by a meta_query using WP_Query and use WordPress's paginate_links() method to load more with your AJAX.
Hope that helps.

PHP Display image issue in WordPress

On this page http://www.cardiffchristmasmarket.com/visit/ i'm trying to get 4 images to display down the side - only 3 images display and when i upload the 4th to the page in WordPress it just removes it instantly. HELP!
PHP of the page below:
<div class="col span_1_of_4">
<div class="page_image">
<?php if( get_field('image_1') ): ?>
<img src="<?php the_field('image_1'); ?>" />
<?php endif; ?>
</div>
<div class="page_image">
<?php if( get_field('image_2') ): ?>
<img src="<?php the_field('image_2'); ?>" />
<?php endif; ?>
</div>
<div class="page_image">
<?php if( get_field('image_3') ): ?>
<img src="<?php the_field('image_3'); ?>" />
<?php endif; ?>
</div>
<div class="page_image">
<?php if( get_field('image_4') ): ?>
<img src="<?php the_field('image_4'); ?>" />
<?php endif; ?>
</div>
</div>
Please check the name "image_4" is match with the id in the custom field
or check the return type of the image in the custom field
Check your wordpress settings, it might be set to display three images only. It may be page settings or widgets, theme settings.
It Seams that you are using ACF (Advanced Custom Fields)
Check this URL for displaying correctly the Images : https://www.advancedcustomfields.com/resources/image/
<?php
$image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
Thanks for your help guys!
The issue lay in the Custom Fields - the field name for image 4 was missing '_'
THANKS!

WP ACF Get Attachment 'Page Link' URL on number of images in slider

I am using Advanced Custom Fields to generate a slider for my site. Each of the images (attachments) also have a custom field attached to them using the 'Page Link' option, which allows me to associate a link with each image. The code below is pulling through a link, but it is the same for each image, regardless of which one is displayed in the slider (each image should have a different link)
<section class="slideshow">
<?php
$images = get_field('slideshow');
if( $images ):
?>
<?php foreach( $images as $image ): ?>
<figure>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<figcaption>
<h2><?php echo $image['caption']; ?></h2>
<h4>View Case Study</h4>
</figcaption>
</figure>
<?php endforeach; ?>
<?php endif; ?>
</section>
I have also tried making the url_link field specific to the image as below, but this doesn't pull any information.
<h4>View Case Study</h4>
Many thanks in advance.
It turns out that this isn't the best use of the 'Gallery' custom field. Instead, I've turned to using the 'Repeater' field which can have sub-fields in which I can hold image, caption and link information. Code amended as follows.
<section class="slideshow">
<?php if( have_rows('slideshow_slides') ): ?>
<?php while( have_rows('slideshow_slides') ): the_row();
// vars
$image = get_sub_field('image');
$caption = get_sub_field('caption');
$link = get_sub_field('link');
?>
<figure>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<figcaption>
<h2><?php echo $caption; ?></h2>
<?php if( $link ): ?>
<h4>View Case Study</h4>
<?php endif; ?>
</figcaption>
</figure>
<?php endwhile; ?>
<?php endif; ?>
</section>

ACF Image in Repeater not working

For a custom WP theme I am creating I am using ACF. I need to have a repeater that outputs images. I have it set to image object and using the correct code. In fact I tried it without a repeater and it works perfectly. Its only within the repeater that it fails.
<div class="row col-md-12">
<?php
// check if the repeater field has rows of data
if( have_rows('pics') ):
// loop through the rows of data
while ( have_rows('pics') ) : the_row();
// display a sub field value
?><div class="col-md-4">
<?php
$image = get_field('img');
if( !empty($image) ): ?>
<img src="<?php echo $image; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div> <?
endwhile;
else :
// no rows found
endif;
?>
</div>
What is causing the image data to not loop?
Your code looks ok, but having checked how I did it I believe you should be using get_sub_field instead of get_field
http://www.advancedcustomfields.com/resources/get_sub_field/
Your comment about using the ['url'] part of the array is also relevant. This is working for me;
$image = get_sub_field('img');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
I think this is what you have to do:
<?php
// check if the repeater field has rows of data
if( have_rows('img') ):
// loop through the rows of data
while ( have_rows('img') ) : the_row();
// display a sub field value
$image = get_sub_field('sub_img'); ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endwhile;
else :
// no rows found
endif;
?>
This is assuming that you are outputting an image object and not a url in your ACF settings.

Categories