WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] php Sessions and last 5 page id's (2 posts)

  1. formica
    Member
    Posted 1 year ago #

    Hi there

    Just attempting to store and use the last 5 page id's in a session. Being new to it I'm stuck and can only get it to print the current page id. ie it's not storing the previous ones in the array. Am I doing something obviously wrong

    global $wp_query;
    $post=$wp_query->get_queried_object();
    
    session_start();
    is_array($_SESSION["last_5_urls"]) or $_SESSION["last_5_urls"] = array();
    
    $_SESSION["last_5_urls"][] = $post->ID;
    // This appends it right onto an array.
    
    $_SESSION["last_5_urls"] = array_slice($_SESSION["last_5_urls"], -5);
    
    Print_r ($_SESSION["last_5_urls"]);

    Any pointers appreciated. Thanks.

  2. formica
    Member
    Posted 1 year ago #

    Got it working by splitting the above code ie. placing the session in the 'header.php' then printing it where needed in the sidebar. From there you can use the post id's to display what you need.

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic