首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ACF Repeater字段|加载更多内容

ACF Repeater字段|加载更多内容
EN

Stack Overflow用户
提问于 2016-09-21 15:49:42
回答 1查看 3.1K关注 0票数 4

我正在遵循一个Github代码来添加更多的负载到ACF中继器字段。我有101个字段,但加载更多不起作用。我也试过装窃听器。Ajax响应为0。可能是通过ajax,它不工作。我是否必须在functions.php文件上添加任何内容。Ajax响应为0。

代码语言:javascript
复制
<?php 

    /*
        The code in this file is an example off the code that you would use in your template to
        show the first X number of rows of a repeater

        I'm sure there are more elegant ways to do the JavaScript, but what's here will work
    */

    if (have_rows('gallery_work', 'option')) {
        // set the id of the element to something unique
        // this id will be needed by JS to append more content
        $total = count(get_field('gallery_work', 'option'));
        ?>
            <ul id="my-repeater-list-id">
                <?php 
                    $number = 2; // the number of rows to show
                    $count = 0; // a counter
                    while( have_rows('gallery_work', 'option') ):the_row();
                        //the_row();
                        $image_se_work = get_sub_field('image_se_work', 'option');
                        ?>
                            <li><img src="<?php echo $image_se_work;?>" alt=""></li>
                        <?php 
                        $count++;
                        if ($count == $number) {
                            // we've shown the number, break out of loop
                            break;
                        }
                    endwhile; // 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:void(0);" onclick="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
                    $.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
                            $('#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
                                $('#my-repeater-show-more-link').css('display', 'none');
                            }
                            console.log(json);
                        },
                        'json'
                    );
                }

                console.log(<?php echo $total;?>);

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

?>
EN

回答 1

Stack Overflow用户

发布于 2016-10-14 10:23:49

这里只有两个文件示例中的一个文件。这是模板中的代码。本例的PHP端位于另一个PHP文件https://github.com/Hube2/acf-dynamic-ajax-select-example/tree/master/repeater-ajax-load-more中,其中包括您需要添加到functions.php文件中的WP AJAX操作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39610499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档