[英]Passing PHP Variables to AJAX Loaded Pages
我有一个博客,每页显示9
博客文章,并使用Infinite Ajax Scroll插件自动添加博客文章的每个其他页面。
每次成功查询博客帖子时, $postCount
变量$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
。
如果这是第一篇文章,我需要锻炼逻辑以销毁会话。
任何想法欢迎。 谢谢。
通过在我的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
的关键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.