微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从wordpress主题模板文件发出ajax请求

如何解决如何从wordpress主题模板文件发出ajax请求

我正在尝试从我的自定义主题中的模板文件进行 ajax 调用。最初,我遵循了另一个教程,该教程让我将在 admin-ajax.PHP 文件调用 ajax 时将运行的函数放在 admin 文件夹中。这正在工作,但显然不是最好的去处,因为每次更新 wordpress 时它都会被覆盖。所以我想把它移到我的主题文件夹中。

这里是 page-talent.PHP 文件中的工作 ajax 调用

$.ajax({
      type: 'POST',url: '<?PHP echo admin_url('admin-ajax.PHP');?>',dataType: "html",// add data type
      data: { action : 'get_ajax_posts',filters: filters },success: function( response ) {
          console.log( response );

          //alert(response);

          $( '.posts-area' ).html( response ); 
      }
  });      
})

这是当前在 admin-ajax.PHP 文件中的代码

function get_ajax_posts() {

    // get filters from 3 drop down menus 
    $tax_query = $_POST['filters'];
    
    $location = $tax_query['location'];
    $specialty = $tax_query['specialty'];

    $levels = $tax_query['level'];
    // create levels array for selected level and above
    switch ($levels) {
            case 23:
                $level = array('23'); // Level 1 through 5
                break;
            case 24:
               $level = array('24'); // Level 2 through 5
                break;
            case 25:
                $level = array('25');// Level 3 through 5
                break;
            case 26:
                $level = array('26');// Level 4 and 5
                break;
            case 27:
                $level = '27';// Level 5
                break;
            default:
                $level = array('23','24','25','26','27');
        }

    // display Talent if only Level is selected
    if(isset($tax_query['level']) && empty($tax_query['location']) && empty($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title','order' => 'ASC','post_type' => 'the-talent','posts_per_page'=>-1,'tax_query' => array(
              'relation' => 'AND',array(
                  'taxonomy' => 'level','field'    => 'term_id','terms'    => $level,// 23 (4),24(4),25(7),26(3),27(3) // array( 25,26,27 )
                  
              ),),);
    }
    // display Talent if only Level and Location is selected
    else if(isset($tax_query['level']) && isset($tax_query['location']) && empty($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',array(
                  'taxonomy' => 'location','terms'    => $location,);
    }
    // display Talent if all three are selected
    else if(isset($tax_query['level']) && empty($tax_query['location']) && isset($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',27 )  
              ),array(
                  'taxonomy' => 'specialty','terms'    => $specialty,);
    }
    // display Talent if Level and specialty is selected
    else if(isset($tax_query['level']) && isset($tax_query['location']) && isset($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',);
    }
    // display Talent if only Location is selected
    else if(empty($tax_query['level']) && isset($tax_query['location']) && empty($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',);
    }
    // display Talent if Location and specialty is selected
    else if(empty($tax_query['level']) && isset($tax_query['location']) && isset($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',);
    }
    // display Talent if only specialty is selected
    else if(empty($tax_query['level']) && empty($tax_query['location']) && isset($tax_query['specialty'])){

        // Query Arguments
        $args = array(
          'orderby' => 'title',);
    }
    
    else{
        $args = null;
        //echo "else Args: ". $args;
    }

    wp_reset_query();

    // The Query
    $ajaxposts = new WP_Query( $args );

    $response = '';

    // The Query
    if ( $ajaxposts->have_posts() ) {
        while ( $ajaxposts->have_posts() ) {
            $ajaxposts->the_post();
            //$response .= get_template_part('products');

            $response .= "";

            $name = get_field('name');
            $main_image = get_field('main_image');

         ?>

        <div class="col-sm-6 col-md-3 talent-col">
          <div class="talent">
            <a type="button" href="<?PHP the_permalink() ?>">
             <img class="img-responsive" src="<?PHP echo $main_image; ?>">
             <h3 class="dark"><?PHP echo $name; ?></h3> 
            </a>
          </div><!-- close talent -->
        </div><!-- close col -->

       <?PHP
     
       }// end while
    } else {
        $response .= get_template_part('none');
    }
    
    exit; // leave ajax call
}// end get_ajax_posts

// Fire AJAX action for both logged in and non-logged in users
add_action('wp_ajax_get_ajax_posts','get_ajax_posts');
add_action('wp_ajax_nopriv_get_ajax_posts','get_ajax_posts');

有人说要将此添加到我的functions.PHP文件中,我这样做了,但是如何从Ajax请求中调用它?

或者另一个建议是在我的主题文件夹中创建一个 Talent-ajax.PHP 文件,但是我如何从 ajax 调用链接到它?我试过了,但没有用……

  $.ajax({
          type: 'POST',//url: '<?PHP //echo admin_url('admin-ajax.PHP');?>',url: '<?PHP echo bloginfo('template_directory')."/talent-ajax.PHP";?>',// add data type
          data: { action : 'get_ajax_posts',success: function( response ) {
              console.log( response );

              //alert(response);

              $( '.posts-area' ).html( response ); 
          }
      });      
    })

当我运行这个时,我收到一个 500 服务器错误

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。