当用户点击相应按钮时,我创建了一个AJAX函数来“显示更多”自定义帖子类型。该函数工作并填充页面但返回空值/元素。我目前正在使用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();
});
Functions.php中的AJAX处理程序
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 vars来测试数据是否被拉出并填充,只是不包含在所需的区域中。
答案 0 :(得分:0)
使用/list
就像撰写@RequestMapping(value = { "/newuser" }, method = RequestMethod.POST)
public String saveUser(@Valid User user, BindingResult result,
ModelMap model) {
if (result.hasErrors()) {
return "registration";
}
if(!userService.isUserSSOUnique(user.getId(), user.getSsoId())){
FieldError ssoError =new FieldError("user","ssoId",messageSource.getMessage("non.unique.ssoId", new String[]{user.getSsoId()}, Locale.getDefault()));
result.addError(ssoError);
return "registration";
}
userService.saveUser(user);
model.addAttribute("success", "User " + user.getFirstName() + " "+ user.getLastName() + " registered successfully");
model.addAttribute("loggedinuser", getPrincipal());
return "redirect:/list";
}
一样,因此它只对内联显示数据有用:the_field()
要将这些字段分配给变量并使用它们来构建更大的字符串,您将要使用echo $field
代替,并且您可能需要传递员工的ID作为第二个参数,如<h6><?php the_field('employee_name') ?></h6>