dongye7231
2017-02-17 09:48
浏览 35
已采纳

Wordpress:为每个用户显示具有相同网址的页面

I'm trying to create a members area inside a website made with WordPress but I'm stuck with a problem.

In this member area, there is a page (for example 'My Products) where I want to show different pieces of information for each user.

I'd like to create a page "My Products" for each user called by the same URL in the menu.

So if I click on "My Products" in the menu, I want to see the products for the currently logged user. Can you help me?

Thanks in advance.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在使用WordPress制作的网站内创建一个成员区域,但我遇到了问题。

在此会员区中,有一个页面(例如“我的产品”),我希望为每个用户显示不同的信息。

我想为菜单中相同网址调用的每个用户创建一个“我的产品”页面。

因此,如果单击菜单中的“我的产品”,我想查看当前登录用户的产品。 你能帮助我吗?

提前致谢。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanao2688 2017-02-20 13:57
    已采纳

    so here is the way i've done it :

    You need to change "XXXX" with your page id, and "user_custom_page" with your user meta key.

    add_filter( 'request', 'custom_request', 1 );
    function custom_request($query) {
        global $wpdb;
    
        if( substr($_SERVER['SERVER_PROTOCOL'], 0, 5) == "HTTP/" )
            $current_link = "http://";
        else
            $current_link = "https://";
        $current_link .= $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    
        $user_page = get_permalink( XXXX );
        $user_id = get_current_user_id();
    
        if($current_link == $user_page && $user_id !== 0) {
            $custom_user_page_id = get_user_meta($user_id, "user_custom_page", true);
            if(!empty($custom_user_page_id)) {
    
                $p_g = $_GET;
                $p_ru = $_SERVER['REQUEST_URI'];
                $p_qs = $_SERVER['QUERY_STRING'];
    
    
                $_SERVER['REQUEST_URI'] = "/?post_type=page&p=".$custom_user_page_id;
                $_SERVER['QUERY_STRING'] = "post_type=page&p=".$custom_user_page_id;
                $_GET = array(
                    'post_type' => 'page',
                    'p' => $custom_user_page_id
                );
    
                remove_filter( 'request', 'custom_request', 1 );
                global $wp;
                $wp->parse_request();
                $query = $wp->query_vars;
                add_filter( 'request', 'custom_request', 1 );
    
                $_SERVER['REQUEST_URI'] = $p_ru;
                $_SERVER['QUERY_STRING'] = $p_qs;
                $_GET = $p_g;
            }
        }
    
        return $query;
    }
    

    EDIT : This code will be in the functions.php of the theme.

    点赞 打赏 评论

相关推荐 更多相似问题