我有以下代码用于获取、过滤和显示输入时的自定义帖子(onkeyup)。然而,它目前不起作用;打开控制台时,我还得到一个500(内部服务器错误)。我错过了什么?
PHP:` add\\u action(\'wp\\u ajax\\u data\\u fetch\',\'data\\u fetch\');add\\u action(\'wp\\u ajax\\u nopriv\\u data\\u fetch\',\'data\\u fetch\');函数data\\u fetch(){
$the_query = new WP_Query( array( \'posts_per_page\' => 6, \'s\' => esc_attr(
$_POST[\'keyword\'] ), \'post_type\' => \'custom_post\' ) );
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ): $the_query->the_post(); ?>
<div class="cell s_result_box">
<div class="title"><a href="<?php echo esc_url( post_permalink() ); ?>"><?php
the_title();?></a></li></div>
<div class="content"><?php the_content(); ?></div>
<div class="meta"><span class=""><?php the_post_meta() ?></span> - <span
class=""><?php get_the_date(); ?></span></div>
<div class="dividerline"></div>
</div>
</div>
<?php endwhile; ?>
add_action( \'wp_footer\', \'ajax_fetch\' );
function ajax_fetch() {
?>
<script type="text/javascript">
function fetchResults() {
var keyword = jQuery(\'#searchInput\').val();
if(keyword == "" || keyword == 0){
jQuery(\'#datafetch\').html( " " );
jQuery("#search-message").fadeIn();
return;
} else {
jQuery("#search-message").fadeOut();
jQuery.ajax({
url: \'<?php echo admin_url(\'admin-ajax.php\'); ?>\',
type: \'post\',
data: { action: \'data_fetch\', keyword: keyword },
success: function(data) {
jQuery(\'#datafetch\').html( data );
}
});
}
}
</script>
//The search form and results div
<form class="t_margin_0" autocomplete="off">
<input type="search" name="s" class="t_margin_0 dir_search"
placeholder="Search by name, location, department…" id="searchInput"
onkeyup="fetchResults()">
</form>
<div class="grid-x" id="datafetch"></div>`