【问题标题】:How to show a users last visited pages in wp / buddypress?如何在 wp / buddypress 中显示用户上次访问的页面?
【发布时间】:2020-02-20 21:51:26
【问题描述】:

我试图弄清楚如何在我的网站中显示一个人访问过的最后 3-5 个页面。我做了一些搜索,但我找不到这样做的 WP 插件,如果有人知道,请指出我的方向 :) 如果没有,我将不得不从头开始编写它,这就是我的位置需要帮助。

我一直在尝试了解数据库及其工作原理。我假设这就是使用 PHP 发生魔法的地方,除非有一个使用 cookie 的 javascript 选项来做到这一点。

我对所有想法持开放态度:P 和谢谢

【问题讨论】:

标签: php javascript jquery wordpress buddypress


【解决方案1】:

如果我要编写这样一个插件,我会使用会话 cookie 通过 array_unshift() 和 array_pop() 填充一个数组。很简单:

$server_url = "http://mydomain.com";
$current_url = $server_url.$_SERVER['PHP_SELF'];
$history_max_url = 5; // change to the number of urls in the history array

//Assign _SESSION array to variable, create one if empty ::: Thanks to Sold Out Activist for the explanation!
$history = (array) $_SESSION['history'];

//Add current url as the latest visit
array_unshift($history, $current_url);
//If history array is full, remove oldest entry
if (count($history) > $history_max_url) {
    array_pop($history);
}
//update session variable
$_SESSION['history']=$history;

现在我已经动态地编写了这个代码。可能存在语法错误或拼写错误。如果出现这样的错误,只需发出通知,我会修改它。这个答案的目的主要是为了证明概念。您可以根据自己的喜好进行调整。请注意,我假设 session_start() 已经在您的代码中。

希望对你有帮助。

================

嘿!很抱歉回答迟了,我出城几天了! :)

这个插件是为了回答您对带有 LI 标签的打印解决方案的要求

这就是我要做的:

print "<ol>";
foreach($_SESSION['history'] as $line) {
     print "<li>".$line.</li>";
}
print "</ol>"; 

就这么简单。你应该在这里阅读 foreach 循环:http://www.php.net/manual/en/control-structures.foreach.php

至于 session_start();,把它放在你使用任何 $_SESSION 变量之前。

希望对您有所帮助! :)

【讨论】:

  • 没有人记得 PHP 的动态变量。 "$history = (array) $_SESSION['history'];"跳过所有“如果(计数”)爵士乐。
  • PHP 会根据其内容自动键入变量,而不管它以前是什么。 $test = 1 变成一个数字,$test = "string" 现在是一个字符串。您可以帮助进行类型转换。 $test = (string) 1 将是一个字符串。 "$history = (array) $_SESSION['history'];"将会话变量转换为数组,如果它为空,PHP 只会创建一个空数组。
  • 我知道我们可以使用 array() 声明数组,但从未遇到过 (array) 语法。清晰的解释,朋友!非常感谢!
  • 由于 PHP 的动态类型转换,很少需要使用手动类型转换。但是,在其他语言中,这是每天都会发生的事情。在这里,它只是减少逻辑门并将代码执行时间缩短几毫秒的捷径。
  • 你好!首先,感谢您花时间回答我!我正在阅读代码,试图了解到底发生了什么。不过,我不得不承认,我不确定 session_start() 的去向,或者它是否已启动。另外,还有一件事,假设我希望将其打印在
  • 标签中,我该怎么做?我对PHP不是很精通,所以请原谅。再次感谢:)
【解决方案2】:

我将为 WordPress 5+ 更新和翻译上面的代码,因为原始问题具有 wordpress 标签。请注意,您在任何地方都不需要session_start()

在这里,将以下代码添加到您的 singular.php 模板(或 single.php + page.php 模板,具体取决于您的需要):

/**
 * Store last visited ID (WordPress ID)
 */
function so7035465_store_last_id() {
    global $post;

    $postId = $post->ID; // or get the post ID from your template

    $historyMaxUrl = 3; // number of URLs in the history array
    $history = (array) $_SESSION['history'];

    array_unshift($history, $postId);

    if (count($history) > $historyMaxUrl) {
        array_pop($history);
    }

    $_SESSION['history'] = $history;
}

// Display latest viewed posts (or pages) wherever you want
echo '<ul>';
    foreach ($_SESSION['history'] as $lastViewedId) {
        echo '<li>' . get_permalink($lastViewedId) . '</li>';
    }
echo '</ul>';

您还可以通过在您的single-cpt.php 模板中放置so7035465_store_last_id() 函数来存储最新查看的自定义帖子类型(CPT)。

您也可以将其添加到挂钩或将其作为操作注入模板中,但这超出了本问题的范围。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签