Last Viewed Posts
[resolved] Show on view of second page after cookie is set. (3 posts)

  1. joshuaiz
    Posted 2 years ago #


    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"])) { ?>
        <h3>Recently Viewed:</h3>
    <?php zg_recently_viewed(); ?>
    <?php }  endif; ?>

    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?



  2. joshuaiz
    Posted 2 years ago #

    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])) { ?>
    				<h3>Recently Viewed:</h3>
    				<?php zg_recently_viewed(); ?>
    			<?php }
  3. joshuaiz
    Posted 1 year ago #

    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(); ?>
    		<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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Last Viewed Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic