«Загрузить ещё» поля повторителя через AJAX - PullRequest
0 голосов
/ 01 декабря 2018

Я получаю ошибку acf POST http://sekilsati/wp-admin/admin-ajax.php 400 (Bad Request) acf версии 5.7

Я не нашел ничего, что работает, если код не работает.нравится новая версия, но я не знаю почему.Пожалуйста, не узнайте, сколько времени вы можете помочь.WordPress версия 9,8

single.php

<?php
if (have_rows('sekil')) {
    // set the id of the element to something unique
    // this id will be needed by JS to append more content
    $total = count(get_field('sekil'));
    ?>
        <ul id="my-repeater-list-id">
            <?php 
                $number = 2; // the number of rows to show
                $count = 0; // a counter
                while (have_rows('sekil')) {
                    the_row();
                    ?>
                        <li>sdfsdfdf</li>
                    <?php 
                    $count++;
                    if ($count == $number) {
                        // we've shown the number, break out of loop
                        break;
                    }
                } // end while have rows
            ?>
        </ul>
        <!-- 
            add a link to call the JS function to show more
            you will need to format this link using
            CSS if you want it to look like a button
            this button needs to be outside the container holding the
            items in the repeater field
        -->
        <a id="my-repeater-show-more-link" href="javascript: my_repeater_show_more();"<?php 
            if ($total < $count) {
                ?> style="display: none;"<?php 
            }
            ?>>Show More</a>
        <!-- 
            The JS that will do the AJAX request
        -->
        <script type="text/javascript">
            var my_repeater_field_post_id = <?php echo $post->ID; ?>;
            var my_repeater_field_offset = <?php echo $number; ?>;
            var my_repeater_field_nonce = '<?php echo wp_create_nonce('my_repeater_field_nonce'); ?>';
            var my_repeater_ajax_url = '<?php echo admin_url('admin-ajax.php'); ?>';
            var my_repeater_more = true;

            function my_repeater_show_more() {

                // make ajax request
                jQuery.post(
                    my_repeater_ajax_url, {
                        // this is the AJAX action we set up in PHP
                        'action': 'my_repeater_show_more',
                        'post_id': my_repeater_field_post_id,
                        'offset': my_repeater_field_offset,
                        'nonce': my_repeater_field_nonce
                    },
                    function (json) {
                        // add content to container
                        // this ID must match the containter 
                        // you want to append content to
                        jQuery('#my-repeater-list-id').append(json['content']);
                        // update offset
                        my_repeater_field_offset = json['offset'];
                        // see if there is more, if not then hide the more link
                        if (!json['more']) {
                            // this ID must match the id of the show more link
                            jQuery('#my-repeater-show-more-link').css('display', 'none');
                        }
                    },
                    'json'
                );
            }

        </script>
    <?php       
} // end if have_rows

?>

function.php

// add action for logged in users
add_action('wp_ajax_my_repeater_show_more', 'my_repeater_show_more');
// add action for non logged in users
add_action('wp_ajax_nopriv_my_repeater_show_more', 'my_repeater_show_more');

function my_repeater_show_more() {
    // validate the nonce
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'my_repeater_field_nonce')) {
        exit;
    }
    // make sure we have the other values
    if (!isset($_POST['post_id']) || !isset($_POST['offset'])) {
        return;
    }
    $show = 4; // how many more to show
    $start = $_POST['offset'];
    $end = $start+$show;
    $post_id = $_POST['post_id'];
    // use an object buffer to capture the html output
    // alternately you could create a varaible like $html
    // and add the content to this string, but I find
    // object buffers make the code easier to work with
    ob_start();
    if (have_rows('sekil', $post_id)) {
        $total = count(get_field('sekil', $post_id));
        $count = 0;
        while (have_rows('sekil', $post_id)) {
            the_row();
            if ($count < $start) {
                // we have not gotten to where
                // we need to start showing
                // increment count and continue
                $count++;
                continue;
            }
            ?><li>xfgdfg</li><?php 
            $count++;
            if ($count == $end) {
                // we've shown the number, break out of loop
                break;
            }
        } // end while have rows
    } // end if have rows
    $content = ob_get_clean();
    // check to see if we've shown the last item
    $more = false;
    if ($total > $count) {
        $more = true;
    }
    // output our 3 values as a json encoded array
    echo json_encode(array('content' => $content, 'more' => $more, 'offset' => $end));
    exit;
} 
...