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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀