【发布时间】:2014-12-11 06:26:48
【问题描述】:
我有一个博客,每页显示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 加载页面上的计数。因此,博客文章的下一页将从 10 开始,而不是重置为 0。
我明白为什么它不起作用(变量只是本地变量),我需要使用 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。
如果这是第一篇文章,我需要锻炼逻辑以销毁会话...
欢迎任何想法。谢谢。
【问题讨论】:
-
嘿@Machavity 我不确定我是否理解这里的重复。你介意详细说明吗?谢谢。
-
该主题讨论了如何在 Wordpress 中打开 PHP 会话。此时您可以使用您描述的会话。
-
@Machavity 感谢您的跟进。我认为这是不同的,因为我的问题不是严格来说如何在 WordPress 中使用
sessions,而是如何在 ajax 加载页面的范围内使用它们。
标签: php jquery ajax wordpress session