我有一个自定义主题,我使用下划线起始主题构建。在我的一个模板文件(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\');?>\',