2017-07-18 126 views
0

当用户单击相应的按钮时,我创建了一个AJAX函数以“显示更多”自定义帖子类型。该函数工作并填充页面,但返回空值/元素。我目前正在使用ACF,并尝试将数据存储在我的functions.php中使用的变量中,但不幸的是没有成功。有什么想法吗?Ajax POST结果显示空值 - ACF

初始脚本排队功能

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">&times;</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

使用the_field()是这样写echo $field所以它仅用于显示数据内嵌有用:<h6><?php the_field('employee_name') ?></h6>

为了分配这些领域的一个变量,并使用它们来构建一个更大的字符串,你要使用get_field('employee_name'),而不是和您可能需要将员工ID作为第二个参数get_field('employee_name', $employee_id=100)

+0

我明白了,我更关注您对get_field()的响应。看到我可以传递和false参数来获取当前的帖子ID。我也意识到我有循环外的变量。一旦我使用适当的函数将变量放入循环中,它就会根据需要填充。 – WeebleWobb

+0

另请阅读一些有用的文档:https://www.advancedcustomfields.com/resources/get_field/和https://www.advancedcustomfields.com/resources/the_field/。感谢您指点我正确的方向。 – WeebleWobb