【发布时间】:2018-06-30 15:29:07
【问题描述】:
我创建了一个在页面中传递 ID 的 ajax 文件。并且该页面有一个查询。我得到了一个错误。我试图知道我的错误是什么,有些网站说我必须定义我的模板并需要 wp-load.php,但有些网站说我必须在 function.php 上做。我真的不知道该怎么办。我是 wordpress woocommerce 的新手。
未捕获的错误:在 xxxx/xxxx 中找不到类“WP_Query”
这是我的页面 (get-fabric.php)
<?php
if(!empty($_POST['select_colour']))
{
$args = array(
'post_type' => 'product',
'posts_per_page' => 1000,
'product_cat' => 'fabric'
);
$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
global $product;
$regular_price = $product->get_price_html();
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $query->post->ID ), 'product' );
echo ' <div class="col-md-3 fabric-active" data-fabcode="32860">
<div class="fabric-cloth">
<div class="fabric-data">
<img src="'.$image[0].'" class="img-responsive cursor-on" />
<div class="fabric-code">
<p>'.$product->post->post_title.'</p>
<span>'.$regular_price.'</span>
</div>
</div>
</div>
</div>';
endwhile;
wp_reset_query();
?>
<div class="col-md-3 fabric-active" data-fabcode="32860">
<div class="fabric-cloth">
<div class="fabric-data">
<img src="'.$image[0].'" class="img-responsive cursor-on" />
<div class="fabric-code">
<p>'.$product->post->post_title.'</p>
<span>'.$regular_price.'</span>
</div>
</div>
</div>
</div>
<?php
}
?>
【问题讨论】:
-
您的代码中还有其他一些错误。 1)你应该用
$product = wc_get_product($query->post->ID);替换global $product;…2)你应该用$query->post->post_title替换$product->post->post_title… -
好的 LoicTheAztec 我已经改了,谢谢兄弟的帮助
标签: php ajax wordpress woocommerce