我创建了一个在页面中传递 ID 的 ajax 文件。该页面有一个查询。我得到了一个错误。我想知道我的错误是什么,有些网站说我必须定义我的模板并需要 wp-load.php,但有些网站说我必须在 function.php 上做。我真的不知道该怎么办。我是 wordpress woocommerce 的新手。
Uncaught Error: Class 'WP_Query' not found in xxxx/xxxx
这是我的页面 (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
}
?>
最佳答案
如果您直接调用此 url,这不是 WordPress ajax 的预期工作方式,您仍然可以通过添加此 php 代码来运行此代码
$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );
到文件的顶部。
https://codex.wordpress.org/AJAX_in_Plugins我知道这是说插件中的 Ajax,但这也是您在开发自定义主题以保持代码 WordPress-y 时会做的事情。
关于php - Uncaught Error : Class 'WP_Query' not found in url - Wordpress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48374145/