【问题标题】:How to get userid by url wordpress in userpro plugin?如何在 userpro 插件中通过 url wordpress 获取用户 ID?
【发布时间】:2017-07-01 11:15:08
【问题描述】:

我正在使用 userPro 插件。我的网址如下:

http://www.example.com/profile/alex/

(alex 是用户名,我在个人资料页面我需要在function.php 文件中获取 alex)

我在管理员端的永久链接设置中设置了帖子名称

如果我从永久链接设置中设置它默认,那么我可以获得用户 ID 和 url 将像这样

http://www.example.com/?page_id=522&up_username=alex

如果我写$_GET['up_username'] 那么我会得到用户名。我想在永久链接设置中使用 帖子名称

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您必须使用 Wordpress 的构建功能 get_query_var() 来执行此操作,因为您使用的是基于 POSTNAME 的永久链接,您应该替换:

    $_GET['up_username'];
    

    get_query_var('up_username');
    

    希望对你有所帮助。

    【讨论】:

      【解决方案2】:

      这将为您获取 Userpro 中的用户对象:

      $user = $userpro->get_member_by( get_query_var('up_username') );
      print_r ($user);
      echo '<br />';
      echo ($user->user_login);
      echo '<br />';
      echo ($user->user_nicename);
      

      【讨论】:

        【解决方案3】:

        这是@sweet potato 给出的答案的混合,但是当登录的用户在个人资料页面中时,我包含了函数viewing_his_profile()。

        当您想在 userpro 的模板之外添加一些代码时非常有用

        希望对你有帮助

        global $userpro;
        if ($userpro->viewing_his_profile()) {
            debug(get_current_user_id());
        } else {
            $user = $userpro->get_member_by(get_query_var('up_username'));
            debug($user->data->ID);
        }
        

        【讨论】:

          猜你喜欢
          • 2016-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-19
          相关资源
          最近更新 更多