用户元数据的Ajax分页

时间:2015-08-02 19:18:55

标签: ajax wordpress pagination

如果我的脚本搜索user取决于用户元数据,如何在我的AJAX中设置分页链接。分页工作正常,但问题在于分页链接不起作用。有人可以帮我弄清楚如何正确设置分页链接吗?

这是我的AJAX脚本:

function nanny_search_ajax1() {
$search_in = $_POST['search_in'];
$search_type = $_POST['search_type'];
$start_date = $_POST['start_date'];
$keyword = $_POST['keyword'];


if($search_in=='nanny'){
    $search_in ='interested';
}
else{
    $search_in ='lookgin';
}

$start_value = $start_date;
$start_compare = '=';


$nanny_result = '';
$nanny_error = '';


$number     = 2; 
$paged      = (get_query_var('paged')) ? get_query_var('paged') : 1;
$offset     = ($paged - 1) * $number;

$args = array(
    'meta_query' => array(
        'relation' => 'AND',            
        array(
            'key' => 'user_start',
            'value' => $start_value,
            'compare' => $start_compare
            )
        ),
    'offset'=>$offset,
    'number'=>$number,
);


$users      = get_users();
$query      = get_users($args);
$total_users = count($users);
$total_query = count($query);
$total_pages = intval($total_users / $number) + 1;


$query = get_users($args);



if (!empty($query)) {
    foreach ($query as $user) {
        $profile_picture = wp_get_attachment_url(get_user_meta($user->ID, 'be_custom_avatar', true));
        $profile_picture = get_avatar($user->ID, 100);
        if($search_in=='nanny'){
            $interested = get_user_meta($user->ID, 'interested', true);
        }
        else{
            $interested = get_user_meta($user->ID, 'lookgin', true);
        }

        $user_date = get_user_meta($user->ID, 'user_start', true);
        $aboutme = get_user_meta($user->ID, 'aboutme', true);
        $user_link = get_author_posts_url($user->ID);           

        $nanny_result .= "<div class='nanny-babysiter-box'>";

        if ($profile_picture != '') {
            $nanny_result .="<div class='nanny-babysiter-box-img'>$profile_picture</div>";
        }

        $nanny_result .="<div class='user-icons'><ul><li><a href='#'><i class='fa fa-circle-o'></i></a></li><li><a href='#'><i class='fa fa-envelope-o'></i></a></li><li><a href='#'><i class='fa fa-star'></i></a></li></ul></div>";


        $nanny_result .="<div class='user-details'><div class='details-sub search-title'><a target='_blank' href='$user_link'><span>$user->display_name</span></a></div>";




        $nanny_result .="<div class='details-sub'>$interested</div>";            


        if ($user_date != '') {
            $nanny_result .="<div class='details-sub'><span>Start Date : </span>$user_date</div>";
        }
        if ($aboutme != '') {
            $nanny_result .="<div class='details-sub'><span>About Me : </span></div>";
        }
        $nanny_result .="</div>";
        if ($aboutme != '') {
            $nanny_result .="<div class='details-sub'>$aboutme</span></div>";
        }

        $nanny_result .="</div>";



    }
    if ($total_users > $total_query) {
                $nanny_result.='<div id="pagination" class="clearfix">';
                $nanny_result.='<span class="pages">Pages:</span>';
                  $current_page = max(1, get_query_var('paged'));
                  $nanny_result.= paginate_links(array(
                        'base' => get_pagenum_link(1) . '%_%',
                        'format' => 'page/%#%/',
                        'current' => $current_page,
                        'total' => $total_pages,
                        'prev_next'    => false,
                        'type'         => 'list',
                    ));
                $nanny_result.= '</div>';
            }
} else {
    $nanny_error = '<div>Unfortunately at present there is no one matching your exact search criteria. You may like to consider other candidates.</div>';
}

echo json_encode(array("nanny_result" => $nanny_result, "nanny_error" => $nanny_error));
die;
}

add_action('wp_ajax_nopriv_nanny_search_ajax1', 'nanny_search_ajax1');
add_action('wp_ajax_nanny_search_ajax1', 'nanny_search_ajax1');

提前感谢。

1 个答案:

答案 0 :(得分:1)

我怀疑你的分页链接也应该是ajaxified,所以你可能会使用像$('.prev-page').click(function(){ ... });这样的smth来定位它们。如果是这种情况 - 在页面加载和DOM解析之后,您的分页链接将添加到DOM,因此您的click事件不会绑定到目标元素。要将javascript事件绑定到动态创建/添加的DOM元素,请使用on()delegate()方法,例如$('.prev-page').on('click', function(){ ... })。有关详细信息,请参阅文档。