0

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.

Every time a blog post is successfully queried the $postCount variable increases its count by 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. So, instead of resetting to 0 the next page of blog posts would start at 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.

I'm guessing I need to start a session on my index.php page pass that along to my IAS plugin call...

Any help greatly appreciated. Thanks!


Follow up 1:

I've had some luck getting PHP sessions to work by starting the session in my functions.php file

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.

I'll need to workout the logic to destroy the session if it's the first post...

Any thoughts welcome. Thanks.

Rich
  • 1,136
  • 3
  • 16
  • 36
  • possible duplicate of [Wordpress session management](http://stackoverflow.com/questions/1441240/wordpress-session-management) – Machavity Dec 10 '14 at 19:47
  • Hey @Machavity I'm not sure I understand the duplication here. Would you mind elaborating? Thanks. – Rich Dec 10 '14 at 19:53
  • That thread discusses how to turn PHP sessions on in Wordpress. At which point you can use the sessions like you describe. – Machavity Dec 10 '14 at 20:32
  • @Machavity Thanks for the follow up. I think this is different though, since my question wasn't strictly how to use `sessions` in WordPress and more how to use them within the scope of an ajax loaded page. – Rich Dec 10 '14 at 20:50

1 Answers1

0

I was able to keep the $postCount variable counting by starting a php session in my functions.php file

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.

Rich
  • 1,136
  • 3
  • 16
  • 36