Home>

<>
1. Save the viewed article with cookie ← Done
2. Display recommended articles based on the tag IDs of frequently viewed articles from the viewed articles saved in cookies.

/ **
 * Cache settings
 * /
add_action ('get_header','readpost');
function readpost () {
global $browser_histories;
$browser_histories = null;
$set_this_ID = null;
    if (is_single ()) {
if (isset ($_ COOKIE ['postid_history'])) {
// The postid_history part can be any string
// call the cookie value
$browser_histories = explode (",", $_COOKIE ['postid_history']);
if ($browser_histories [0]! = get_the_ID ()) {
if (count ($browser_histories)>= 50) {
$set_browsing_histories = array_slice ($browser_histories, 0, 49);
} else {
$set_browsing_histories = $browser_histories;
}
// Add to the beginning of the string if the value does not start with the current article ID
$set_this_ID = get_the_ID ().','.implode (",", $set_browsing_histories);
setcookie ('postid_history', $set_this_ID, time () + 60 * 60 * 24 * 365 * 1,'/');
//} else {
// $set_this_ID = $_COOKIE ['postid_history'];
}
} else {
// Save the current article ID if there is no cookie
$set_this_ID = get_the_ID ();
setcookie ('postid_history', $set_this_ID, time () + 60 * 60 * 24 * 365 * 1,'/');
}
// Call only except for details page
} else {
if (isset ($_ COOKIE ['postid_history'])) {
$browser_histories = explode (",", $_COOKIE ['postid_history']);
}
}
$postread = explode (",", $_COOKIE ['postid_history']);
$postread = array_unique ($postread);
$postread = array_values ​​($postread);
return $postread;
}


You can even save your browsing history.
From there, I'm trying to display recommended articles based on my browsing history, but I can't figure out how to do it.

  • Answer # 1

    There is no cookie part in the code in the first place.
    First, decide the data specification of the cookie, and write the code to get and set it with PHP.

Related articles