WordPress ACF 'post-object' 和 WooCommerce 产品 = 首页错误?



我有这个WP_Query:

$query = new WP_Query(
array(
'posts_per_page' => $postsCount,
'post_type' => 'events',
'taxonomy' => 'events-category',
'meta_key' => 'event-start-date',
'orderby' => $postsOrderBy,
'order' => $postsOrder,
'meta_query' => array(
array(
'key' => 'event-start-date',
'compare' => '>=',
'value' => date("Y-m-d"),
'type' => 'DATE',
),
),
),
);
if ($query->have_posts()):
while ($query->have_posts()): $query->the_post();
// content
endwhile;
wp_reset_query();
endif;

在"内容"中,我有这样的东西:

<?php
$post_object = get_field('event-ticket');
if( $post_object ):
$post = $post_object;
setup_postdata( $post );
$product->get_id(); ?>
<div class="e-post__details-item e-post__details-item--event-price"><i class="e-post__details-item--icon fas fa-ticket-alt"></i><?php woocommerce_get_template( 'single-product/price.php' ); ?></div>
<div class="l-cta l-cta--events">
<div class="e-post__event-stock-status">
<?php if ( $product->is_in_stock() ) {
echo '<div class="e-post__event-stock-status-available">'.__('Dostępne!', 'woocommerce').'</div>';
} elseif ( ! $product->is_in_stock() ) {
echo '<div class="e-post__event-stock-status-unavailable">'.__('Wyprzedane!', 'woocommerce').'</div>';
} ?>
</div>
</div>
<?php
wp_reset_postdata();
endif;
?>

get_field('event-ticket'( 是 ACF 帖子对象字段类型,带有"发布对象"返回对象。此字段指向WooCommerce产品以获取价格和数量库存。

WP_Query是CPT"事件"的循环。在每次事件中,我都使用帖子对象字段指向WooCommerce产品,这是一个票证。

问题: 来自"event-ticket"的数据适用于每个子页面(档案,分类,页面,cpt帖子(,但不适用于首页(主页(:

Uncaught Error
: Call to a member function get_id() on null

可能是什么原因?

您以前没有声明过$product。这就是您收到此错误的原因。$product->get_id();您之前未初始化$product。

也许你首先想要:

$product = new WC_Product($post->ID);

我弄清楚:我只是在$product->get_id((之前添加全局$product;现在它正在:)

最新更新