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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制