全局$post变量在single-product.php中始终被覆盖

全局$post变量在single-product.php中始终被覆盖,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我想在选项卡的单个页面上显示3个相关产品。我进行了如下自定义查询: $postid = array(1,2,3); $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'post__in' => array_column($postid, 'ID'), ); $loop = new WP_QUERY($args); 此查询工

我想在选项卡的单个页面上显示3个相关产品。我进行了如下自定义查询:

$postid = array(1,2,3);

$args = array(
  'post_type'         => 'product',
  'post_status'       =>  'publish',
  'post__in'          =>  array_column($postid, 'ID'),

);

$loop = new WP_QUERY($args);
此查询工作并执行3次。但在循环中,我调用另一个模板

wc_get_template_part( 'content', 'single-product-meta-side' );
在content-single-product-meta-side.php中,$post变量被重置,并且只返回原始的查询post变量

global $product, $post;
echo $post_id = get_the_ID(); // old/original post id returns
我还试图
setup\u postdata($post)就在while循环之后,但什么也没发生

你知道怎么回事吗。 另一个查询如何设置全局$product变量。

函数使用。最后,需要具有一些全局变量的模板文件,以确保WordPress环境在函数中可用:

global $product, $post;
echo $post_id = get_the_ID(); // old/original post id returns
global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
因此,您可以使用上述全局变量,也可以找到该文件并将其包含在范围中:

include(locate_template('single-product-meta-side.php'));//Don't forget to set the right path for your file.

我希望这会对您有所帮助。

通常您会使用
global
从函数内部引用在函数外部声明的变量。它本身并不意味着使用
global
声明的变量是全局变量。我还尝试将全局变量放在所有函数之前的页面顶部。但什么也没发生