WP:使用 cookie 将用户重定向到某个类别 [英] WP: redirect users to a certain category using cookies

查看:31
本文介绍了WP:使用 cookie 将用户重定向到某个类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上就像cragslist.一旦您在 craigslist 上选择了城市,下次您访问该网站时,它会将您重定向到您选择的城市.

Basically like cragslist. once you select city on craigslist, nexttime when you go to the site, it redirects you to the city you selected.

我想要实现的目标:当一个人来到网站并选择特定类别时,他们下次访问网站时(回访用户)- 页面将在该类别部分打开.

What I want to achieve: When a person comes to the site and selects a particular category, the next time they come to the site (returning user) - the page will open up on that category section.

我认为通过在访问者单击类别链接时(或在类别页面加载时)设置 cookie 来实现这一点相当容易.当他们在接下来的时间返回时,cookie 被读取,然后页面相应地重定向.

I would think this would be fairly easy to do via setting a cookie when the visitor clicks on the category link (or when the category page loads). When they return the following time, the cookie is read and then the page redirects accordingly.

不幸的是,我对 PHP 和 cookie 的了解有限(因此我在寻找答案)所以我需要询问是否有人可以帮助我!

Unfortunately my knowledge of PHP and cookies is limited, (hence my search for answers) so I need to ask if anyone can help me out!

有人有什么想法吗?

谢谢!

推荐答案

markratledge 有一个很好的链接,但 WordPress 有一个内置函数来重定向用户和传递 http 状态代码,以及它自己的首选 cookie 设置方法.

markratledge has a good link, but WordPress has a built in function to redirect users and pass an http status code as well as it's own preferred method of settings cookies.

试一试.我不太确定它是否有效,因为我现在无法测试它,但它应该为您指明正确的方向.

Give this a shot. I'm not too sure if it works, because I can't test it out right now, but it should point you in the right direction.

function user_cat()
{
    //Check to see if our cookie is set
    if(isset($_COOKIE['visitorhome']))
    {
        //Redirect to the link defined in the cookie
        wp_redirect($_COOKIE['visitorhome'], 302);
    }
    else
    {
        //If it's a category page than get the current URL set the cookie with it.
        if(is_category())
        {
            $user_cat = get_permalink();
            setcookie("visitorhome", $user_cat, time()+86400, "/", str_replace('http://www','',get_bloginfo('url')) );
        }
    }
}
add_action('init', 'user_cat');

这篇关于WP:使用 cookie 将用户重定向到某个类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆