我使用WordPress構建自定義產品頁面(page1.php)。使用ajax從wordpress數據庫獲取產品
我在自定義產品頁面(page1.php)上使用Ajax來調用其他頁面,其中包含下面的代碼(page2.php),使用下面的代碼從wordpress數據庫獲取產品。
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'hoodies'
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
global $product;
echo '<br /><a>' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
endwhile;
wp_reset_query();
?>
上述實際代碼工作正常,當我不把它通過AJAX(即直接從www.localhost/WordPress的/使page2.php加載它),但是當我把它通過Ajax的page1.php中,我得到以下錯誤;
致命錯誤:未捕獲的錯誤:\ XAMPP \ htdocs中\ WordPress的,完全定製\可溼性粉劑內容\主題\店面\使page2.php:9堆棧跟蹤:#0類 'WP_Query' 沒有發現在C {main}拋出第9行C:\ xampp \ htdocs \ wordpress-fully-custom \ wp-content \ themes \ storefront \ test-page2.php
請問我該如何解決這個問題?
感謝
你在哪裏導入'WP_Query'文件第2頁? – OPV
@OPV我沒有這個文件,所以我不知道如何包含它的任何想法 – mark
你最好試試wordpress處理ajax請求的方式。使用本機wp-ajax.php文件。 –