@debendra What if my field is in a repeater?
Ok, in way of an update, I’ve done a bit more investigation and discovered the following. Using <?php echo get_field('custom_bg', $product->ID); ?>
correctly displays the image from the custom field – YAY!
However, this only works on certain products! Right now, during testing, I only have two active Woocommerce products, with IDs 98 and 106. I’ve added some debug code to the page so I know what the current page ID is at any time. When I visit product 106, the debug code correctly shows 106, and the custom_bg field is displayed. And when I visit product 98 I see 98 in the debug code but no custom_bg field displays. What’s most perplexing is when I visit my Woocommerce Shop page, the debug code shows 98.
Is this expected behaviour? Is the ID clash what’s stopping the custom_bg field from displaying?
I’m going to add another product to see if it displays there…
Thanks @hube2, it was the second of those two that worked 🙂
@trouille2 I’m having this issue too, but I can’t get your fix to work. The field I want to call is being used a bit like a featured image, and it lives in the header file. I’ve made a new header called ‘header-shop.php’ and I’m calling this on my Woocommerce shop page. However it’s not pulling in the correct field.
In your example above, how did you get the ‘MY-ARCHIVE-ID’? I know the page id, but not the archive id.
Thanks
@hube2 The fields are located in a custom post type called “sidebars”, and you can pull in any one of these, so how do I get the ID of the sidebar that’s been chosen?
Thanks for the reply @hube2. I’m getting that value from a field in the backend that is a drop-down selector. This selects from a post type called ‘Sidebars’. The idea is that you can create any number of sidebars, add content into them, then add them onto any page from the drop-down. As I mentioned, this works 100% on any standard page outside of Woocommerce.
In answer to your second question, the example I pasted above is in the generic sidebar.php file which is pulled into all page templates. It’s not wrapped in any kind of post loop.
Thanks
Ah, I’ve sorted it!
I only needed to add the posts page ID to the first call, like this:
<?php $post_object = get_field('prospectus', 674);
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<div class="side-box side-prospectus">
<img src="<?php the_field('image'); ?>" alt="<?php the_title(); ?>">
<h3><?php the_title(); ?></h3>
<p><?php the_field('description'); ?></p>
<?php
$file = get_field('pdf_file');
if( $file ): ?>
<a href="<?php echo $file['url']; ?>" target="blank" class="button small button-icon-right button-icon-download"><?php _e('Download (PDF)','anglocontinental') ?></a>
<?php endif; ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Solved it. For anyone else wondering, this is how I did it:
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'portfolio',
'showposts' => 1,
'orderby' => 'rand',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'client_quote',
'value' => '',
'compare' => '!='
)
)
);
$client_quote = new WP_Query( $args );
?>
That randomly picks one post from the Portfolio custom post type, as long as the client_quote custom field contains some text.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.