Ajax POST 结果显示空值 - ACF
Ajax POST results show empty values - ACF
我创建了一个 AJAX 函数来 "Show More" 自定义 post 类型,当用户单击相应的按钮时。该函数有效并填充了页面,但 returns 为空 values/elements。我目前正在使用 ACF 并试图将数据存储在我的 functions.php 中使用的变量中,但不幸的是没有成功。有什么想法吗?
初始脚本排队功能
function synchrony_theme_scripts() {
wp_enqueue_script('main-js', get_template_directory_uri() . '/js/script.js', array('jquery'), '', true);
wp_localize_script('main-js', 'team_ajax', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));
}
Jquery AJAX 函数
var count = 0;
function load_more_team(count) {
var button = $('#more_posts'),
count = count + 12,
data = {
'action': 'ajax_more_team',
'offset': count
}
$.ajax({
url: team_ajax.ajaxurl,
data: data,
type: 'POST',
dataType: 'html',
success: function(data){
if(data.length){
$("#ajax_posts").append(data);
button.attr("disabled",false);
} else{
button.attr("disabled",true);
}
}
});
return false;
}
$('#more_posts').click(function() {
$("#more_posts").attr("disabled",true);
load_more_team();
});
AJAX Functions.php
中的处理程序
function ajax_more_team($offset) {
$offset = $offset + 12;
header("Content-Type: text/html");
$args = array(
'post_type' => 'team',
'posts_per_page' => 12,
'offset' => $offset
);
$the_query = new WP_Query($args);
$id = get_the_id();
$headshot = the_field('employee_headshot');
$name = the_field('employee_name');
$title = the_field('employee_title');
$company = the_field('employee_company');
$url = get_template_directory_uri();
$out = '';
if ($the_query -> have_posts()) : while ($the_query -> have_posts()) : $the_query -> the_post();
$id = get_the_id();
$name = the_field('employee_name');
$out .= '<div class="col-6 col-md-4 col-lg-3 col-xl-2 mb-4">
<div class="team-member">
<a href="#" data-toggle="modal" data-target="#'.$id.'">
<img class="img-fluid" src="'.$headshot.'" alt="'.$headshot.'">
</a>
<div class="team-info">
<h6>'.$name.'</h6>
</div>
<a href="" data-toggle="modal" data-target="#myModal">
<div class="modal-icon">
<img class="img-fluid" src="'.$url.'/imgs/modal-icon.svg">
</div>
</a>
</div>
<!-- Modal -->
<div class="modal fade" id="'.$id.'" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="team-close-btn">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<img class="img-fluid" src="'.$headshot.'" alt="Alice George">
<div class="team-info">
<h6>'.$name.'</h6>
<p><strong>Title:<br></strong>'.$title.'</p>
<p><strong>Company:<br></strong>'.$company.'</p>
</div>
</div>
</div>
</div>
</div>
</div>';
endwhile;
endif;
wp_reset_postdata();
die($out);
}
add_action('wp_ajax_nopriv_ajax_more_team', 'ajax_more_team');
add_action('wp_ajax_ajax_more_team', 'ajax_more_team');
?>
我在循环中添加了 $id 和 $name 变量来测试数据是否被拉取并填充,只是没有包含在所需的区域。
使用 the_field()
就像写 echo $field
所以它只对内联显示数据有用:<h6><?php the_field('employee_name') ?></h6>
要将这些字段分配给变量并使用它们构建更大的字符串,您需要使用 get_field('employee_name')
代替,您可能需要将员工的 ID 作为第二个参数传递喜欢 get_field('employee_name', $employee_id=100)
我创建了一个 AJAX 函数来 "Show More" 自定义 post 类型,当用户单击相应的按钮时。该函数有效并填充了页面,但 returns 为空 values/elements。我目前正在使用 ACF 并试图将数据存储在我的 functions.php 中使用的变量中,但不幸的是没有成功。有什么想法吗?
初始脚本排队功能
function synchrony_theme_scripts() {
wp_enqueue_script('main-js', get_template_directory_uri() . '/js/script.js', array('jquery'), '', true);
wp_localize_script('main-js', 'team_ajax', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));
}
Jquery AJAX 函数
var count = 0;
function load_more_team(count) {
var button = $('#more_posts'),
count = count + 12,
data = {
'action': 'ajax_more_team',
'offset': count
}
$.ajax({
url: team_ajax.ajaxurl,
data: data,
type: 'POST',
dataType: 'html',
success: function(data){
if(data.length){
$("#ajax_posts").append(data);
button.attr("disabled",false);
} else{
button.attr("disabled",true);
}
}
});
return false;
}
$('#more_posts').click(function() {
$("#more_posts").attr("disabled",true);
load_more_team();
});
AJAX Functions.php
中的处理程序function ajax_more_team($offset) {
$offset = $offset + 12;
header("Content-Type: text/html");
$args = array(
'post_type' => 'team',
'posts_per_page' => 12,
'offset' => $offset
);
$the_query = new WP_Query($args);
$id = get_the_id();
$headshot = the_field('employee_headshot');
$name = the_field('employee_name');
$title = the_field('employee_title');
$company = the_field('employee_company');
$url = get_template_directory_uri();
$out = '';
if ($the_query -> have_posts()) : while ($the_query -> have_posts()) : $the_query -> the_post();
$id = get_the_id();
$name = the_field('employee_name');
$out .= '<div class="col-6 col-md-4 col-lg-3 col-xl-2 mb-4">
<div class="team-member">
<a href="#" data-toggle="modal" data-target="#'.$id.'">
<img class="img-fluid" src="'.$headshot.'" alt="'.$headshot.'">
</a>
<div class="team-info">
<h6>'.$name.'</h6>
</div>
<a href="" data-toggle="modal" data-target="#myModal">
<div class="modal-icon">
<img class="img-fluid" src="'.$url.'/imgs/modal-icon.svg">
</div>
</a>
</div>
<!-- Modal -->
<div class="modal fade" id="'.$id.'" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="team-close-btn">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<img class="img-fluid" src="'.$headshot.'" alt="Alice George">
<div class="team-info">
<h6>'.$name.'</h6>
<p><strong>Title:<br></strong>'.$title.'</p>
<p><strong>Company:<br></strong>'.$company.'</p>
</div>
</div>
</div>
</div>
</div>
</div>';
endwhile;
endif;
wp_reset_postdata();
die($out);
}
add_action('wp_ajax_nopriv_ajax_more_team', 'ajax_more_team');
add_action('wp_ajax_ajax_more_team', 'ajax_more_team');
?>
我在循环中添加了 $id 和 $name 变量来测试数据是否被拉取并填充,只是没有包含在所需的区域。
使用 the_field()
就像写 echo $field
所以它只对内联显示数据有用:<h6><?php the_field('employee_name') ?></h6>
要将这些字段分配给变量并使用它们构建更大的字符串,您需要使用 get_field('employee_name')
代替,您可能需要将员工的 ID 作为第二个参数传递喜欢 get_field('employee_name', $employee_id=100)