简体   繁体   English

将PHP变量传递到AJAX加载的页面

[英]Passing PHP Variables to AJAX Loaded Pages

I have a blog that shows 9 blogs posts per page, with each additional page of blog posts automatically added using the Infinite Ajax Scroll plugin. 我有一个博客,每页显示9博客文章,并使用Infinite Ajax Scroll插件自动添加博客文章的每个其他页面。

Every time a blog post is successfully queried the $postCount variable increases its count by 1 . 每次成功查询博客帖子时, $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>

I'd like for the $postCount variable to remember its count on the AJAX loaded pages as well. 我想让$postCount变量也记住它在AJAX加载页面上的计数。 So, instead of resetting to 0 the next page of blog posts would start at 10 . 因此,与其重置为0 ,下一页博客文章将从10开始。

I understand why it's not working (variables are only local) and that I'll need to use something like sessions or get , but I don't know how to implement that. 我了解为什么它不起作用(变量仅在本地),并且我需要使用诸如sessionsget类的东西,但我不知道如何实现。

I'm guessing I need to start a session on my index.php page pass that along to my IAS plugin call... 我猜我需要在index.php页面上开始一个session ,并将其传递给我的IAS插件调用...

Any help greatly appreciated. 任何帮助,不胜感激。 Thanks! 谢谢!


Follow up 1: 跟进1:

I've had some luck getting PHP sessions to work by starting the session in my functions.php file 我很幸运,可以通过在我的functions.php文件中启动会话来使PHP会话正常工作

function init_session() {
    session_start();
}
add_action( 'init', 'init_session', 1 );

...and adding the sessions to my file like so: ...然后将会话添加到我的文件中,如下所示:

[...]
<body> 

   <div class="posts">
        <?php

        $postCount = 0;

        if ( isset ( $_SESSION[ 'postCount' ] ) ) :
            $postCount = $_SESSION[ 'postCount' ];
        endif;

        [...] // wp_query

        $_SESSION['postCount'] = $postCount;

        ?>
    </div> <!-- /posts -->

</body>

As it stands the $postCounter keeps counting... So what was initially considered post 1 could be post 100 when the page is refreshed. 就目前而言, $postCounter一直在计数...因此,最初认为发布1可能是刷新页面时发布的100

I'll need to workout the logic to destroy the session if it's the first post... 如果这是第一篇文章,我需要锻炼逻辑以销毁会话。

Any thoughts welcome. 任何想法欢迎。 Thanks. 谢谢。

I was able to keep the $postCount variable counting by starting a php session in my functions.php file 通过在我的functions.php文件中启动php会话,我能够保持$postCount变量计数

function init_session() {
    session_start();
}
add_action( 'init', 'init_session', 1 );

...and setting up my loop like so: ...并像这样设置我的循环:

[...]
<body> 

   <div class="posts">
        <?php

        $postCount = 0;

        if ( is_paged() ) :
            $postCount = $_SESSION[ 'postCount' ];
        endif;

        [...] // wp_query

        $_SESSION['postCount'] = $postCount;

        ?>
    </div> <!-- /posts -->

</body>

Using is_paged() is the key to keep the counter at 0 after a refresh. 使用is_paged()是刷新后将计数器保持为0的关键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM