[英]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.