【发布时间】:2014-04-07 04:25:30
【问题描述】:
我有 Ajax 调用来获取响应,在响应文件中我使用了 wordpress 循环使用 WP_Query() 类... 但是当我执行 ajax 它返回致命错误: 致命错误:第 20 行的 C:\xampp\htdocs\business-finder\wp-content\themes\businessfinder\metabox\ajax-process.php 中找不到类 'WP_Query'
这里是 AJAX 调用代码:
var path = 'http://localhost/business-finder/wp-content/themes/businessfinder/metabox/ajax-process.php';
$.ajax({
type: "POST",
url: path,
data: { param:folio_data }
}).done(function( msg ) {
$( '#ajax_folio' ).html( msg );
//alert( "Data Saved: " + msg );
});
这是响应文件代码:
<?php
print_r( $_POST['param'] );
if( !empty( $_POST['param'] ) ):
echo spyropress_get_attached_posts1( $_POST['param'], 'ait-grid-portfolio' );
echo '<br><br>';
endif;
function spyropress_get_attached_posts1( $post_id = '', $post_type = '' ){
//if( $post_id = '' || $post_type = '' ) return;
$counter = 0;
global $wp_query ;
$query = new WP_Query( array( 'post_type' => $post_type, 'post__in' => $post_id ) );
if( $query->have_posts() ):
$out .= '<table border = "1">';
while( $query->have_posts() ):
$query->the_post();
if( $counter == 6 ):
$out .= '<tr class = "post_list">';
else:
$counter++;
endif;
$out .= '<td>'.get_the_post_thumbnail(get_the_ID(), array(100,100)).'<br>'.get_the_title().'</td>';
if( $counter == 6 ):
$out .= '</tr>';
$counter = 0;
endif;
endwhile;
$out .= '</table>';
wp_reset_postdata();
else:
$out = 'No Posts Found....';
endif;
wp_reset_query();
return $out;
}
add_action( 'init', 'spyropress_get_attached_posts' );
?>
【问题讨论】:
-
你完全错了。阅读:codex.wordpress.org/AJAX_in_Plugins
标签: javascript jquery ajax wordpress function