AJAX调用 - 无法识别wordpress函数/类

时间:2014-03-05 08:23:38

标签: javascript jquery ajax wordpress function

我有Ajax调用获取响应,在响应文件中我使用了wordpress循环usng 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' );
?>

1 个答案:

答案 0 :(得分:0)

您写错了ajax代码。你不能像这样使用ajax。见下面的代码:

jQuery(document).ready(function($) {

 var dataString = {
                action: 'my_ajax',
                param: folio_data
            };
            jQuery.ajax
            ({
                type: "POST",
                url: "<?php  echo admin_url('admin-ajax.php'); ?>",
                data: dataString,
                cache: false,
                success: function(msg)
                {  
                     $( '#ajax_folio' ).html( msg );
                    //alert( "Data Saved: " + msg );

                }
            });
  });

 function ajaxDataSubmit(){
  global $wpdb; 
  $post_id = $_POST['param'];
   $post_type='ait-grid-portfolio';
   $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();
die($out);
}
add_action('wp_ajax_my_ajax', 'ajaxDataSubmit');//Logged in users
add_action('wp_ajax_nopriv_my_ajax', 'ajaxDataSubmit'); // Not logged in uNOTE: mer