来自主题目录的AJAX调用

时间:2020-12-29 作者:Aaron4osu

我有一个自定义主题,我使用下划线起始主题构建。在我的一个模板文件(page talent.php)中,我有一个jQuery ajax函数:

   $.ajax({
          type: \'POST\',
          url: \'<?php echo admin_url(\'admin-ajax.php\');?>\',
          dataType: "html", // add data type
         // data: { action : \'get_ajax_posts\' },
          data: { action : \'get_ajax_posts\' , filters: filters },
          success: function( response ) {
              console.log( response );
              //alert(response);
              $( \'.posts-area\' ).html( response ); 
          }
      });      
    })
然后我在admin ajax中运行代码。管理文件夹中的php。我不知道为什么我会这样做(而不是在我的主题目录中的某个地方),除了这个论坛上的一些帖子所说的那样。

问题是,当wordpress更新时,它会将其清除。所以问题是,我怎样才能把它放在我的主题中呢?我尝试调用主题文件夹中的一个文件,但没有成功。

这是我放在现有管理ajax中的代码。php就在do\\u操作(“admin\\u init”)之前;部分同样,在这个文件中,它工作得很好,但每次我更新wordpress的版本时,它都会被删除。

// ------------------------------
// ------------------------------
//       talent query code
// ------------------------------
// ------------------------------

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\',
          \'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 )
                  
              ), 
              array(
                  \'taxonomy\' => \'location\',
                  \'field\'    => \'term_id\',
                  \'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\',
          \'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 )  
              ), 
              array(
                  \'taxonomy\' => \'specialty\',
                  \'field\'    => \'term_id\',
                  \'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\',
          \'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 )  
              ), 
              array(
                  \'taxonomy\' => \'location\',
                  \'field\'    => \'term_id\',
                  \'terms\'    => $location,
              ),
              array(
                  \'taxonomy\' => \'specialty\',
                  \'field\'    => \'term_id\',
                  \'terms\'    => $specialty,
              ),  
          ),
        );
    }
    // 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\',
          \'order\' => \'ASC\',
          \'post_type\' => \'the-talent\',
          \'posts_per_page\'=>-1,
          \'tax_query\' => array(
              \'relation\' => \'AND\', 
              array(
                  \'taxonomy\' => \'location\',
                  \'field\'    => \'term_id\',
                  \'terms\'    => $location,
              ),  
          ),
        );
    }
    // 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\',
          \'order\' => \'ASC\',
          \'post_type\' => \'the-talent\',
          \'posts_per_page\'=>-1,
          \'tax_query\' => array(
              \'relation\' => \'AND\',
              array(
                  \'taxonomy\' => \'location\',
                  \'field\'    => \'term_id\',
                  \'terms\'    => $location,
              ),
              array(
                  \'taxonomy\' => \'specialty\',
                  \'field\'    => \'term_id\',
                  \'terms\'    => $specialty,
              ),  
          ),
        );
    }
    // 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\',
          \'order\' => \'ASC\',
          \'post_type\' => \'the-talent\',
          \'posts_per_page\'=>-1,
          \'tax_query\' => array(
              \'relation\' => \'AND\',
              array(
                  \'taxonomy\' => \'specialty\',
                  \'field\'    => \'term_id\',
                  \'terms\'    => $specialty,
              ),  
          ),
        );
    }
    
    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\');


//===================
// end talent query code
//===================
如果我将此添加到我的函数中。php文件如何从Ajax函数调用它?或者,如果我只是将函数get\\u ajax\\u posts()移动到主题文件夹中的一个新文件(talent ajax.php),我如何从ajax函数调用它。换句话说。。。此部分将进行哪些更改以访问该文件?

url: \'<?php echo admin_url(\'admin-ajax.php\');?>\', 

2 个回复
SO网友:Simon

永远不要编辑wp admin目录中的任何文件。所有与主题相关的ajax都应该在主题目录中。您可以将PHP代码放在任何文件、函数中。php,或者在那里包含一些新文件,您将只检索ajax CAL,这样会更好。

SO网友:Q Studio

将这些全部添加到主题函数中。php文件,然后重试。。

相关推荐

在插件中,如何使用AJAX更新JSON文件

Context学习插件创建,我想在用户每次单击div外部时保存div的内容。简而言之,其目的是创建记事本的文本备份。我在当地工作。Steps<有些文本是用div编写的(感谢Trumbowyg.js)</在div blur事件中,插件使用ajax发送内容数据</插件函数打开一个json文件,并用新文本更新其内容Issues几个我想我在第三步受阻了。几个小时的信息搜索让我不知所措。Update我尝试使用wp\\u remote\\u get()而不是file\\u get\\u conten