duanlian1320 2018-05-01 11:04
浏览 38

“禁止覆盖WordPress全局变量”虽然代码似乎没问题

<?php foreach ( $upsells as $upsell ) : ?>

            <?php
                $post_object = get_post( $upsell->get_id() );

                setup_postdata( $GLOBALS['post'] =& $post_object );

        ?>

        <?php endforeach; ?>

    <?php woocommerce_product_loop_end(); ?>

This yields

ERROR   Overriding WordPress globals is prohibited on line setup_postdata( $GLOBALS['post'] =& $post_object );

I am working on a theme for thmeforest.net

  • 写回答

1条回答 默认 最新

  • doujing3896 2018-05-01 12:01
    关注

    This is wrong practice.You should use a wp_query to that product.

      add_action('woocommerce_after_single_product_summary', 'show_cross_sell_in_single_product', 30); 
    
    function show_cross_sell_in_single_product(){
            $crosssells = get_post_meta( get_the_ID(), '_crosssell_ids',true);
            $args = array( 
                'post_type' => 'product', 
                'posts_per_page' => -1, 
                'post__in' => $crosssells 
                );
            $products = new WP_Query( $args );
            if( $products->have_posts() ) : 
                echo '<div class="cross-sells"><h2>Cross-Sells Products</h2>';
                woocommerce_product_loop_start();
                while ( $products->have_posts() ) : $products->the_post();
                    wc_get_template_part( 'content', 'product' );
                endwhile; // end of the loop.
                woocommerce_product_loop_end();
                echo '</div>';
            endif;
            wp_reset_query(); }
    

    Code Explaination : all upsell product are added using this function when you just write do_action() its simple.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化