使用ajax时不会显示分页

时间:2015-08-13 09:17:45

标签: php jquery html ajax wordpress

我创建一个ajax,一切都很好。但是当ajax工作时,分页并没有显示出来。它消失了。 这是我的代码。 pagehome.php

<div class="resul-ajax">
                    <ul class="names-results listing-view" id=":0.nr" style="-webkit-user-select: none;">
                        <?php
                        $args = array(
                            'posts_per_page' => 10,
                            'post_type' => 'baby_name',
                            'paged' => ( get_query_var('page') ? get_query_var('page') : 1 ),
                            'orderby' => 'title',
                            'order' => 'ASC',
                        );
                        query_posts($args);
                        ?>

                        <?php
                        $i = 0;
                        while (have_posts()) : the_post();
                            ?>
                            <?php
                            $terms = get_the_terms($post->ID, 'gender');
                            $gender = array();
                            foreach ($terms as $term) {
                                $gender[] = $term->name;
                            }
                            ?>

                            <li <?php
                        if ($i == 0) {
                            echo 'class="old"';
                            $i = 1;
                        } else {
                            echo 'class="even"';
                            $i = 0;
                        }
                            ?>>
                                <a href="<?php the_permalink(); ?>">
                                    <?php
                                    if ($gender[0] == 'Male') {
                                        echo "<span class='result-gender boy'></span>";
                                    } elseif ($gender[0] == 'Female') {
                                        echo "<span class='result-gender girl'></span>";
                                    } else {
                                        echo "<span class='result-gender boygirl'></span>";
                                    }
                                    ?>
                                    <span class="result-name"><?php echo get_the_title(); ?></span>
                                    <span class="result-desc"></span>
                                </a>
                            </li>
                            <?php
                        endwhile;
                        ?>
                    </ul>
                    <section class = "pagination">
                        <?php emm_paginate(); ?>
                        <?php
                        wp_reset_query();
                        ?>
                    </section>
                </div>

Ajax的query.php

jQuery(function ($) {
$('.start-with').click(function () {
    var field3 = $("#field3").attr('class');
    var field3vl = $("#field3").attr('name');
    var page = $(".pagination li .active").attr('id');
    $('.start-with').removeClass('curent-st');
    $(this).addClass('curent-st');
    $.post(ajax_object.ajaxurl, {
        action: 'ajax_action',
        startwith: $(this).attr('id'),
        male: $("#boys:checked").attr('name'),
        female: $("#girls:checked").attr('name'),
        field3: field3,
        field3vl: field3vl,
        page: page,
    }, function (data) {
        $('.resul-ajax').fadeOut(500, function () {
            $('.resul-ajax').html(data);
        });
        $('.resul-ajax').fadeIn(500, function () {
            $('.resul-ajax').html(data);
        });
    });
});
$('#boys').change(function () {
    var field3 = $("#field3").attr('class');
    var field3vl = $("#field3").attr('name');
    var startwith = '';
    var page = $(".pagination li .active").attr('id');
    startwith = $('.curent-st').attr('id');
    $.post(ajax_object.ajaxurl, {
        action: 'ajax_action',
        startwith: startwith,
        male: $("#boys:checked").attr('name'),
        female: $("#girls:checked").attr('name'),
        field3: field3,
        field3vl: field3vl,
        page: page,
    }, function (data) {
        $('.resul-ajax').fadeOut(500, function () {
            $('.resul-ajax').html(data);
        });
        $('.resul-ajax').fadeIn(500, function () {
            $('.resul-ajax').html(data);
        });
    });
});
$('#girls').change(function () {
    var field3 = $("#field3").attr('class');
    var field3vl = $("#field3").attr('name');
    var startwith = '';
    var page = $(".pagination li .active").attr('id');
    startwith = $('.curent-st').attr('id');
    $.post(ajax_object.ajaxurl, {
        action: 'ajax_action',
        startwith: startwith,
        male: $("#boys:checked").attr('name'),
        female: $("#girls:checked").attr('name'),
        field3: field3,
        field3vl: field3vl,
        page: page,
    }, function (data) {
        $('.resul-ajax').fadeOut(500, function () {
            $('.resul-ajax').html(data);
        });
        $('.resul-ajax').fadeIn(500, function () {
            $('.resul-ajax').html(data);
        });
    });
});
$('.pagi-ajax').click(function () {
    var field3 = $("#field3").attr('class');
    var field3vl = $("#field3").attr('name');
    var startwith = '';
    var page = $(this).attr('id');
    $('.pagi-ajax').removeClass('active');
    $(this).addClass('active');
    startwith = $('.curent-st').attr('id');
    $.post(ajax_object.ajaxurl, {
        action: 'ajax_action',
        startwith: startwith,
        male: $("#boys:checked").attr('name'),
        female: $("#girls:checked").attr('name'),
        field3: field3,
        field3vl: field3vl,
        page: page,
    }, function (data) {
        $('.resul-ajax').fadeOut(300, function () {
            $('.resul-ajax').html(data);
        });
        $('.resul-ajax').fadeIn(300, function () {
            $('.resul-ajax').html(data);
        });
    });
});});

这是我的function.php

function query_homepage() {
    wp_enqueue_script('ajax-script', get_stylesheet_directory_uri() . '/library/js/ajax-query.js', array('jquery'), 1.0); // jQuery will be included automatically
    wp_localize_script('ajax-script', 'ajax_object', array('ajaxurl' => admin_url('admin-ajax.php'))); // setting ajaxurl
}

add_action('init', 'query_homepage');

function ajax_action_stuff() {
    // getting variables from ajax post
    $startwith = $_POST['startwith'];
    $male = $_POST['male'];
    $female = $_POST['female'];
    $field3 = $_POST['field3'];
    $field3vl = $_POST['field3vl'];
    $page = $_POST['page'];
    if ($male != "" && $female != "") {
        $gender = array("both-names", $female, $male);
    } elseif ($male != "") {
        $gender = $male;
    } elseif ($female != "") {
        $gender = $female;
    }
    $args = array(
        'post_type' => 'baby_name',
        'posts_per_page' => 10,
        'paged' => $page,
        'orderby' => 'title',
        'order' => 'ASC',
    );
    if ($startwith == '' && $field3 == '') {
        $args['tax_query'][] = array(
            array(
                'taxonomy' => 'gender',
                'field' => 'slug',
                'terms' => $gender,
            ),
        );
    } elseif ($startwith == '') {
        $args['tax_query'][] = array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'gender',
                'field' => 'slug',
                'terms' => $gender,
            ),
            array(
                'taxonomy' => $field3,
                'field' => 'slug',
                'terms' => $field3vl,
            ),
        );
    } elseif ($field3 == '') {
        $args['tax_query'][] = array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'gender',
                'field' => 'slug',
                'terms' => $gender,
            ),
            array(
                'taxonomy' => 'starts-with',
                'field' => 'slug',
                'terms' => $startwith,
            ),
        );
    } else {
        $args['tax_query'][] = array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'gender',
                'field' => 'slug',
                'terms' => $gender,
            ),
            array(
                'taxonomy' => 'starts-with',
                'field' => 'slug',
                'terms' => $startwith,
            ),
            array(
                'taxonomy' => $field3,
                'field' => 'slug',
                'terms' => $field3vl,
            ),
        );
    }
    // query babyname
    $the_query = new WP_Query($args);
    // doing ajax stuff
    ?>
    <ul class="names-results listing-view" id=":0.nr" style="-webkit-user-select: none;">
        <?php
        while ($the_query->have_posts()) {
            $the_query->the_post();
            ?>
            <?php
            $terms = get_the_terms($post->ID, 'gender');
            $gender = array();
            foreach ($terms as $term) {
                $gender[] = $term->name;
            }
            ?>
            <li <?php
            if ($i == 0) {
                echo 'class="old"';
                $i = 1;
            } else {
                echo 'class="even"';
                $i = 0;
            }
            ?>>
                <a href="<?php the_permalink(); ?>">
                    <?php
                    if ($gender[0] == 'Male') {
                        echo "<span class='result-gender boy'></span>";
                    } elseif ($gender[0] == 'Female') {
                        echo "<span class='result-gender girl'></span>";
                    } else {
                        echo "<span class='result-gender boygirl'></span>";
                    }
                    ?>
                    <span class="result-name"><?php echo get_the_title(); ?></span>
                    <span class="result-desc"></span>
                </a>
            </li>
            <?php
        }
        ?>
    </ul>
    <section class = "pagination">
        <?php emm_paginate(); ?>  
    </section>
    <?php
    die(); // stop executing script
}

add_action('wp_ajax_ajax_action', 'ajax_action_stuff'); // ajax for logged in users
add_action('wp_ajax_nopriv_ajax_action', 'ajax_action_stuff'); // ajax for not logged in users
?>

我不知道它在哪里出错。我会尝试我所知道但不成功的一切。请帮我。 对不起,因为我的英语非常糟糕。

0 个答案:

没有答案