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
$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 reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.