Support

Account

Home Forums General Issues woocommerce wc_get_template_part fetching page title instead of product title

Helping

woocommerce wc_get_template_part fetching page title instead of product title

  • Hi there!

    I’ve put together this thing where you from the backend select products from woocommerce that you want to display on the home page of my website. The problem is that the products get the home page title instead of their product name. I’m using wc_get_template_part to get the product layout and such. This is how my code looks at the moment:

    <?php if( have_rows('selected_products') ): ?>
    
                            <ul class="w_products products grid-<?php echo $number_by_row; ?>">
    
                                <?php woocommerce_product_loop_start(); ?>
                                <?php 
                                while ( have_rows('selected_products') ) : the_row();
                                    $product_object = get_sub_field('selected_product');
    
                                    setup_postdata( $product_object ); 
    
                                    wc_get_template_part( 'content', 'selectedproduct' );
    
                                    wp_reset_postdata();
                                endwhile;
                                wp_reset_query();
                                ?>
    
                                <?php woocommerce_product_loop_end(); ?>
    
                            </ul>
    
                        <?php endif; ?>

    What can I do to fix it?

  • when you use setup_postdata() the variable name must be $post and it must be called in a way that affect the global $post.

    If you are in a template that is loaded using get_template_part() then the global $post is already set up. If not the you must set it up using

    
    global $post;
    

    And you code needs to look like

    
    $post = get_sub_field('selected_product');
    setup_postdata($post); 
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

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.