我正在为一个拥有三个不同地点的教堂建立一个网站。主页有一些简要信息,然后列出了每个校区。我想存储用户选择的位置,然后在将来访问时将它们重定向到该位置。
例如 - 他们访问 Church.com 并在主页上选择 location2 (church.com/location2)
下次他们在浏览器中输入 Church.com 时,他们会自动重定向到 Church.com/location2
谢谢!
您可以将值存储在cookie中...
当用户访问即位置2时:
<?php
$location = 2;
// setcookie(name, value, expirationTime);
setcookie("location", $location, time() + 2592000); // expiration time of one month
?>
在脚本的开头,您必须检查是否已经设置了“位置”cookie。如果是 -> 重定向到相应的页面
<?php
$location = $_COOKIE["location"];
if(isset($location))
{
header("location: /location".$location);
}
?>
正如我在评论中提到的,我不是 WordPress 专业人士。但是,以下解决方案对我有用。请不要忘记在脚本输出任何内容之前设置您的 cookie。我在模板的 header.php 中放置了以下几行
// get postname (I used postname in 'Permalink Settings')
$location = get_query_var('name');
// if user is on startpage + was not redirected yet -> redirect
// if you don't set the userRedirected cookie, the user is not
// able to visit the startpage anymore to chose a location
if($location == "" && isset($_COOKIE["location"]) && !$_COOKIE["userRedirected"])
{
// path "/" makes the cookie available on the whole domain
setcookie("userRedirected", true, null, "/"); // duration: session
header("location: ".$_COOKIE["location"]); // redirect
}
if($location == "location-1")
{
// set location cookie 1
setcookie("location", "location-1", time() + 2592000, "/");
}
else if($location == "location-2")
{
// set location cookie 2
setcookie("location", "location-2", time() + 2592000, "/");
}
希望能帮助到你