Loop field not outputting images
-
debug info
WordPress 3.9.1
PHP 5.3.27
Apache/2.2.22
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36— Active Plugins —
Custom Field Suite 2.2.2
Disable Comments 1.1
Easy Add Thumbnail 1.1
WordPress Importer 0.6.1This code:
<?php $badges = $cfs->get('badges'); foreach ($badges as $badge) { echo $badge['image']; // a sub-field named "image" echo 'test'; } ?>
is giving me this output :
Warning: Invalid argument supplied for foreach() in……line 100 (line in which the foreach statement is declared)the same code pretty much, works fine on a different page.
Did you set the File Upload field to store the URL or ID? Can you link to a screenshot of your Field Group settings?
Er, I mean an export of the Field Group, not a screenshot haha.
Storing file. so it is not playing nice with other code. in the same page.
If A precedes B then i get an error, otherwise i get all my URL’s nicey without errors.
Code chunk A.<div class="recent-posts"> <?php $the_query = new WP_Query( 'showposts=3' ); ?> <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?> <div class="recent-post"> <a href="<?php the_permalink() ?>"> <?php the_post_thumbnail('frontpage-thumbnail');?> <?php the_title(); ?> </a> </div> <?php endwhile;?> </div>
Code chunk B.
<div class="badges"> <?php $badges = $cfs->get('badges'); foreach ($badges as $badge) { echo $badge['image']; // a sub-field named "image" echo 'test'; } ?> </div>
Export just in case
{"775":{"post_title":"Home","post_name":"home","cfs_fields":[{"id":"2","name":"mission_text","label":"Mission ","type":"textarea","notes":"Your mission statement.","parent_id":0,"weight":0,"options":{"default_value":"Pollination Event Company\u2019s mission is to design and execute smooth running, distinctive events that leave an abiding impression on guests while cross pollinating the public with the agricultural community. We produce events that stand out for their flavor, elegance, and mission-driven character.","formatting":"none","required":"0"}},{"id":"9","name":"media_chooser","label":"Media chooser","type":"select","notes":"Use this to choose whether the Mission Media is a video or image.","parent_id":0,"weight":1,"options":{"choices":{"Image":"Image","YouTube Video":"YouTube Video"},"multiple":"0","required":"1"}},{"id":"3","name":"mission_media","label":"Mission Media","type":"text","notes":"Link to the image or video to be used.","parent_id":0,"weight":2,"options":{"default_value":"","required":"0"}},{"id":"14","name":"badges","label":"Badges","type":"loop","notes":"","parent_id":0,"weight":3,"options":{"row_display":"0","row_label":"Badge","button_label":"Add Image"}},{"id":"15","name":"image","label":"Image","type":"file","notes":"","parent_id":14,"weight":4,"options":{"return_value":"url","required":"0"}}],"cfs_rules":{"post_types":{"operator":"==","values":["page"]},"post_ids":{"operator":"==","values":["760"]},"page_templates":{"operator":"==","values":["homepage-template.php"]}},"cfs_extras":{"order":"0","context":"normal","hide_editor":"1"}}}
Ah ha, in that first snippet, change
<?php endwhile;?>
to be
<?php endwhile; wp_reset_postdata(); ?>
Long story short: your iteration through
WP_Query
was wiping out the global$post
data that$cfs
needs. There are other ways to tell CFS to use a specific post ID (it’s an optional second param ofget()
) but that should resolve this issue.Yup that did it.
So in general when I do a:
$the_query = new WP_Query($things)
Should I i be resetting post data as good practice? or simply keep it under my hat as a debug option?Yes exactly, always a good idea to
wp_reset_postdata()
after looping, every time. Else, as illustrated here, subsequent usage of$post
won’t work right because it’ll be the post data from the last post in yourWP_Query
, not the$post
that loaded with the page. This can create long, frustrating bouts with additional assumptions of the original$post
(e.g. sidebars/footers). I speak from experience haha.Cool beans, every frustrating experience is something I get to add to my small but grows list of knowledges.
RESOLVED.
- The topic ‘Loop field not outputting images’ is closed to new replies.