• Resolved joshuaiz

    (@joshuaiz)


    Hello,

    I’m using this to call the plugin in my template:

    <div id="recentlyviewed" class="clearfix">
    <?php if (function_exists('zg_recently_viewed')):  if (isset($_COOKIE["WP-LastViewedPosts"])) { ?>
        <hr>
        <h3>Recently Viewed:</h3>
    <?php zg_recently_viewed(); ?>
    <?php }  endif; ?>
    </div>

    The problem is, once the cookie is set, it will show the conditional html above after the next page is viewed but there is nothing to show yet as the only last viewed post is the current page.

    Once a user goes to the next page (the second page after the cookie is set), then there is a post to show.

    I’ve tried to run a conditional check to see if there is a value in the second item in the cookie array but not having any luck.

    In plain english:

    Once the cookie is set, only show content if there is a value in the second item in the cookie.

    Does that make sense? How can I do this?

    Thanks.

    http://wordpress.org/plugins/last-viewed-posts/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter joshuaiz

    (@joshuaiz)

    I solved this…what I was missing was to unserialize the cookie data. What I ended up doing was wrapping that in another function so I could use mfunc/dynamic caching with WP Super Cache:

    function vizual_recently_viewed_posts() {
    	if (function_exists('zg_recently_viewed')):  if (isset($_COOKIE["WP-LastViewedPosts"])) {
    		$posts_array = unserialize($_COOKIE["WP-LastViewedPosts"]);
    			if (isset($posts_array[0])) { ?>
    				<hr>
    				<h3>Recently Viewed:</h3>
    
    				<?php zg_recently_viewed(); ?>
    
    			<?php }
    			}
    	endif;
    }
    Thread Starter joshuaiz

    (@joshuaiz)

    Update: the above code still wasn’t working properly with WP Super Cache. I’ve since started using Ajaxize to keep Last Viewed Posts dynamic even with caching.

    To make it work, you need to call both the zg_recently_viewed() function and the zg_lw_setcookie() function to make sure the cookie is set in your custom “ajaxized” function like so:

    function my_recently_viewed_posts() {
    	if (function_exists('zg_recently_viewed')) {
    		zg_lw_setcookie(); ?>
    		<hr>
    		<h3>Recently Viewed:</h3>
    		<?php zg_recently_viewed();
    	}
    }

    Create an ajaxized div in Ajaxize using my_recently_viewed_posts and you are good to go. Works beautifully even with WP Super Cache – the cookie is set and it remains fresh. I haven’t tested this with W3 Total Cache but it should work with that as well.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Show on view of second page after cookie is set.’ is closed to new replies.