Php ACF中继器字段|加载更多
我遵循Github代码向ACF repeater字段添加更多负载。我有101个字段,但加载了更多不工作的字段。我也试着成为一个臭虫。Ajax响应为0。可能是通过ajax,但它不起作用。我是否需要在functions.php文件中添加任何内容。Ajax响应为0Php ACF中继器字段|加载更多,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我遵循Github代码向ACF repeater字段添加更多负载。我有101个字段,但加载了更多不工作的字段。我也试着成为一个臭虫。Ajax响应为0。可能是通过ajax,但它不起作用。我是否需要在functions.php文件中添加任何内容。Ajax响应为0 <?php /* The code in this file is an example off the code that you would use in your template to
<?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
?>
- “alt=”“>
';
var my_repeater_ajax_url='';
var my_repeater_more=true;
函数my_repeater_show_more(){
//发出ajax请求
美元邮政(
我的\u中继器\u ajax\u url{
//这是我们在PHP中设置的AJAX操作
“动作”:“我的中继器”\u显示更多”,
“post\u id”:我的中继器\u字段\u post\u id,
“偏移量”:我的中继器\u字段\u偏移量,
“nonce”:我的\u中继器\u字段\u nonce
},
函数(json){
//向容器添加内容
//此ID必须与容器匹配
//您想将内容附加到
$(“#我的中继器列表id”).append(json['content']);
//更新偏移量
my_repeater_field_offset=json['offset'];
//查看是否有更多链接,如果没有,则隐藏更多链接
如果(!json['more']){
//此ID必须与“显示更多”链接的ID匹配
$(“#我的中继器显示更多链接”).css('display','none');
}
log(json);
},
“json”
);
}
console.log();
这里的2文件示例中只有一个文件。这是模板中的代码。该示例的PHP部分位于另一个PHP文件中,包括您需要添加到functions.PHP文件中的WP AJAX操作