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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度