Home › Forums › General Issues › Post Object Issue with Woocommerce Product Archives
Hi, I have a relational Post Object field for choosing locations in Woocommerce categories. I’ve added the following code to the archive-product.php
<?php
global $post;
$post_object = get_field(‘showsite_location’);
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<? echo get_the_post_thumbnail($post->ID,’thumbnail’);?>
ID); ?>” title=”<? echo get_the_title($post->ID);?>”>How to find us
<?php wp_reset_postdata(); // IMPORTANT – reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
This returns the thumbnail and permalink correctly on the woocommerce category page. However, once you add products to the category, the returned thumbnail and permalink disappear.
How can I get it to stay when products are added ?
Your code maybe resetting the global $post object, please make use of a custom foreach loop as shown in this example:
$post_objects = get_field('post_objects');
if( $post_objects ): ?>
<ul>
<?php foreach( $post_objects as $post_object): ?>
<li>
<a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a>
<span>Post Object Custom Field: <?php the_field('field_name', $post_object->ID); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php endif;
I’ve tried your suggestion but it’s still doesn’t show when products are published on the category page. When there are no products published the foreach just repeats the permalink text over and over.
I managed to solve it with the following code.
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
$post_object = get_field('showsite_location', $post_id);
if( $post_object ):
$post = $post_object ;
setup_postdata($post); ?>
<? echo get_the_post_thumbnail($post,'thumbnail');?>
<a href="<?php echo get_permalink($post); ?>" title="<? echo get_the_title($post);?>">How to find us</a>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
You must be logged in to reply to this topic.
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’re working hard on Advanced Custom Fields PRO 6.0, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) August 12, 2022
Featuring improved performance for Repeater fields with large datasets, and a new generation of ACF Blocks.
Let’s take a look 🧵https://t.co/Befre3kFAo
© 2022 Advanced Custom Fields.
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.