我有一个博客,每页显示9
个博客帖子,每个额外的博客帖子页面都会使用Infinite Ajax Scroll插件自动添加。
每次成功查询博客帖子时,$postCount
变量的计数都会增加1
。
[...]
<body>
<div class="posts">
<?php
$postCount = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post(); $postCount++;
if ( $postCount % 9 == 0 ) :
// show featured blog posts
else :
// show standard blog posts
endif;
endif;
?>
</div> <!-- /posts -->
<script>
var ias = jQuery.ias({
container: '.posts',
item: 'article',
pagination: '.pagination',
next: '.next',
delay: 0,
negativeMargin: 1000
});
</script>
</body>
我希望$postCount
变量能够记住它在AJAX加载页面上的计数。因此,不是重置为0
,而是下一页博客文章将从10
开始。
我理解为什么它不起作用(变量只是本地的)并且我需要使用类似sessions
或get
的内容,但我不知道知道如何实现它。
我猜我需要在index.php页面上启动session
,然后传递给我的IAS插件调用...
任何帮助非常感谢。谢谢!
跟进1:
通过在我的functions.php
文件中启动会话,我有幸运行PHP会话
function init_session() {
session_start();
}
add_action( 'init', 'init_session', 1 );
...并将会话添加到我的文件中,如下所示:
[...]
<body>
<div class="posts">
<?php
$postCount = 0;
if ( isset ( $_SESSION[ 'postCount' ] ) ) :
$postCount = $_SESSION[ 'postCount' ];
endif;
[...] // wp_query
$_SESSION['postCount'] = $postCount;
?>
</div> <!-- /posts -->
</body>
目前$postCounter
一直在计算...因此,当页面刷新时,最初被视为1
帖子的内容可能会发布100
。
如果它是第一篇帖子,我需要训练逻辑来销毁会话...
欢迎任何想法。感谢。
答案 0 :(得分:0)
我能够通过在functions.php文件中启动php会话来保持$postCount
变量的计数
function init_session() {
session_start();
}
add_action( 'init', 'init_session', 1 );
...并设置我的循环:
[...]
<body>
<div class="posts">
<?php
$postCount = 0;
if ( is_paged() ) :
$postCount = $_SESSION[ 'postCount' ];
endif;
[...] // wp_query
$_SESSION['postCount'] = $postCount;
?>
</div> <!-- /posts -->
</body>
使用is_paged()
是刷新后将计数器保持在0
的关键。