I’ve got a YARPP (Yet Another Related Posts Plugin) showing related articles and loading either a custom thumbnail override, an image from a flex content type or a fallback from an options section.
The image object is presented as an array when it loads from the post content, but if the override or options/fallback are loaded, it always returns the image ID instead of an array image object.
Frustratingly, I have this working on two other sites with pretty much the same configuration running the same versions. I’ve spent all day trying to work out what is causing the issue, but I can’t even think how else to troubleshoot the issue.
I’ve completely removed and reinstalled YARPP (including wiping all it’s cache and setting entries in the DB), I’ve reinstalled ACF to the site.
The code being loaded by the YARRP plugin is identical to code being loaded elsewhere (the same partial) and it works in the other places.
Any ideas what is going on here?
YARRP template: (called with <?php related_posts; ?>
<?php if (have_posts()): ?>
<div class="catPostWrapper">
<div class="newsFeed cf" data-maxcolwidth="<?php the_field('maximum_column_width','options'); ?>" data-marginpercent="<?php the_field('margin_percentage','options'); ?>">
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part( 'partials/featured-posts-post' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<!--.newsFeed--></div>
<!--.catPostWrapper--></div>
<?php else:?>
<p>No related stories.</p>
<?php endif; ?>
The relevant code from partials/featured-posts-post:
<div class="colImage userContent">
<?php //get override feature image, or get first image or else use placeholder image
//setup override image if set
if (get_field('feature_image_override'))
{
$image = get_field("feature_image");
}
if( empty($image) ): //if no set override use first post/page image
while(has_sub_field("page_content")):
if(get_row_layout() == "inline_image"): // layout: Image
if(get_sub_field('media_mode') == 'image'): //if image and not video
$image = get_sub_field("image");
break;
endif;
endif;
if(get_row_layout() == "image_video_beside_text"): // layout: layout: Image/Video Beside Text
if(get_sub_field('media_mode') == 'image'): //if image and not video
$image = get_sub_field("image");
break;
endif;
endif;
endwhile;
endif;
if( empty($image) ): //if no images use fallback (set in options)
$image = get_field('image_fallback_default', 'options');
endif;
if( !(empty($image)) ):
$size = 'news-feed-crop';
$alt = $image['alt'];
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
$height = $image['sizes'][ $size . '-height' ];
?>
<a href="<?php the_permalink(); ?>"><img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" /></a>
<?php endif; ?>
<!--.colImage--></div>
Some additional details to this issue:
On top of the image problem. It would seem that the code is ignoring some of the settings from ACF data. Another example is a TEXT AREA field that, instead of following the ‘automatically add paragraphs’ is just outputting br tags.
That being said, when I pull the data from inside the flex content (in the case of the image), that actually does correctly pull the image.
Could the problem lie in the use of ‘get_field’/’the_field’ with YARRP somehow?
Just tried using:
$field_key = "field_54289c0d8173b";
$field = get_field_object($field_key);
print_r($field);
Where the key is the feature_image field.
This resulted in an array output outside of the YARRP template, but in the template the variable was blank.
Please check your field’s settings and make sure that all image fields are set to return an array instead of ID
It is possible that the YARPP plugin is adding custom SQL to any DB querying which would then prevent ACF from finding the field’s settings and correctly formatting the value…
Another option is that ACF is getting confused as to which $post to load the value from. Within the template, can you test the value of get_the_ID()
and check that the value is correct for each related post?
Hey Elliot,
Thanks for getting back to me. The image fields are set to return arrays not IDs, and the post IDs are returning correctly.
I’m leaning towards the idea of something happening with the query too, is there something particular about how the field format settings are stored? It seems to have an issue with the automatically adding paragraphs on text area fields too, instead reverting as if auto BR tags was selected.
I’m just not exactly sure how to isolate/troubleshoot that…
Jumping in on this thread to add that i’ve run into the same issue with YARPP and an ACF image gallery field. When I pull the field anywhere else on my site it returns fine, when i pull the same into a YARPP template, I get an error. Printing the array elsewhere, i get the full image set array (paths, ids, etc.) when i pull it via YARPP, it only returns image ids. Very strange – has anyone had any luck with this the issue?
If it helps, I just stopped using YARPP as I hadn’t seen any updates for ages and was running into other issues.
We now use the premium plugin Related Posts for WordPress Premium by Never5.
I highly recommend it, it’s been a dramatic improvement:
http://www.relatedpostsforwp.com/
So this isn’t a full solution, but it at least allowed me to get the images from the custom image fields to display. Still seems to be a problem with YARPP but this seems to get by the problem at least for images.
I noticed like you said that the image field was returning the ID of the image even though in my case I have it set to return the image URL to be used as a background image.
I just fed that image ID into wp_get_attachement_url() and was able to use that url to display the image.
<?php $imageId = get_field('post_featured_image');
$imageUrl = wp_get_attachment_url($imageId); ?>
<a href="<?php the_permalink(); ?>">
<div class="relatedsingle" style="background-image: url('<?php echo $imageUrl; ?>');">
<div class="singleoverlaygreen"></div>
<h4><?php the_title(); ?></h4>
</div>
</a>
This is obviously only to get the image url for a simple application like mine, but my guess is that you could use wp_get_attachment_metadata() instead of wp_get_attachment_url() to fetch anything from the image array that you would otherwise gotten from the ACF field.
The topic ‘Issue with YARPP plugin loading image array’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.