Support

Account

Home Forums General Issues Post Object Issue with Woocommerce Product Archives

Solved

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 ?

  • Hi @hplwebmaster

    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; ?>
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Post Object Issue with Woocommerce Product Archives’ is closed to new replies.