我在为我正在处理的网站构建自定义分页时遇到了类似的问题。
我在functions.php 中创建的全局变量已定义并设置为0。我可以使用上面概述的@Karsten 方法在我的javascript 中输出这个值。问题在于更新我最初在 PHP 文件中设置为 0 的全局变量。
这是我的解决方法(hacky?我知道!)但在紧迫的期限内挣扎了一个小时后,以下工作:
在 archive-episodes.php 内:
<script>
// We define the variable and update it in a php
// function defined in functions.php
var totalPageCount;
</script>
functions.php 内部
<?php
$totalPageCount = WP_Query->max_num_pages; // In my testing scenario this number is 8.
echo '<script>totalPageCount = $totalPageCount;</script>';
?>
为了简单起见,我通过警报在 $ajax.success 回调中输出 totalPageCount 变量。
$.ajax({
url: ajaxurl,
type: 'POST',
data: {"action": "infinite_scroll", "page_no": pageNumber, "posts_per_page": numResults},
beforeSend: function() {
$(".ajaxLoading").show();
},
success: function(data) {
//alert("DONE LOADING EPISODES");
$(".ajaxLoading").hide();
var $container = $("#episode-container");
if(firstRun) {
$container.prepend(data);
initMasonry($container);
ieMasonryFix();
initSearch();
} else {
var $newItems = $(data);
$container.append( $newItems ).isotope( 'appended', $newItems );
}
firstRun = false;
addHoverState();
smartResize();
alert(totalEpiPageCount); // THIS OUTPUTS THE CORRECT PAGE TOTAL
}
不管怎样,我希望这对其他人有帮助!如果有人有“不那么骇人听闻”的版本或最佳实践示例,我会全力以赴。