这可以按照在WordPress中使用Ajax的常规方法来完成。
https://codex.wordpress.org/AJAX_in_Plugins
首先注册一个脚本文件,并创建一个AJAX调用。
wp_enqueue_script( \'meta-ajaxscript\', get_stylesheet_directory_uri() . \'/js/ajax-init.js\', array( \'jquery\' ), \'\', true );
wp_localize_script( \'meta-ajaxscript\', \'ajaxMeta\', array(
\'ajaxurl\' => admin_url( \'admin-ajax.php\' )
));
其次,按类或ID用父元素包装元数据
<div id="metaWrapper">
<?php
$images = rwmb_meta( \'tj_file_advanced\', \'type=image&size=YOURSIZE\');
$ppp = 3;
foreach (array_slice($images, 0, $ppp) as $image) { ?>
<div class="col-md-4">
<div class="boxe">
<?php echo "<img src=\'{$image[\'full_url\']}\'/>"; ?>
</div>
</div>
<?php } ?>
</div>
<div class="loadmore"></div>
第三,在ajax init中编写一些ajax调用脚本。js。
$( ".loadmore" ).on( "click", function(e) {
e.preventDefault();
var $container = $(\'#metaWrapper\'),
ppp = 3,
number = $container.find( \'> .col-md-4\').length,
page = number / ppp;
$.ajax({
url: ajaxMeta.ajaxurl,
type: \'post\',
data: {
\'page\': page,
\'ppp\': ppp,
action: \'meta_fetch\'
},
success: function( response ) {
if(response == "")
{
// no content was found
$(\'.loadmore\').hide();
}
else
{
console.log(response);
$container.append( response );
}
}
});
return false;
});
现在设置一个PHP函数来处理AJAX请求。
add_action( \'wp_ajax_nopriv_meta_fetch\', \'wpex_metadata_fetch\' );
add_action( \'wp_ajax_meta_fetch\', \'wpex_metadata_fetch\' );
function wpex_metadata_fetch() {
$images = rwmb_meta( \'tj_file_advanced\', \'type=image&size=YOURSIZE\');
$page = (int) $_POST[\'page\'];
$ppp = (int) $_POST[\'ppp\'];
foreach (array_slice($images, $page*$ppp, $ppp) as $image) { ?>
<div class="col-md-4">
<div class="boxe">
<?php echo "<img src=\'{$image[\'full_url\']}\'/>"; ?>
</div>
</div>
<?php
}
die();
}
希望它是有意义的和快乐的编码!