是否将组插件快捷代码列出的用户名转换为显示的名称?

时间:2013-01-25 作者:Iurie

我创建了一个组Groups 插件和我想显示该组的成员列表。我把短码[groups_group_info group="Group Name Here" show="users"] 但它只显示成员的用户名,如下所示:

用户名1

  • 用户名x
    • 我想显示他们的姓名(display_name), 但是插件没有这个选项(我没有找到)。我在插件支持页面上打开了一个帖子,但我还没有收到回复。

      My question is:是否可以使用相应的显示名称(或姓氏)+一些用户元数据更改该短代码列出的用户名?

      下面是呈现短代码的原始代码[groups_group_info group="Group Name Here" show="users"]:

      add_shortcode( \'groups_group_info\', array( __CLASS__, \'groups_group_info\' ) );
      
      public static function groups_group_info( $atts, $content = null ) {
          global $wpdb;
          $output = "";
          $options = shortcode_atts(
              array(
                  \'group\' => \'\',
                  \'show\' => \'\',
                  \'format\' => \'\',
                  \'single\' => \'1\',
                  \'plural\' => \'%d\'
              ),
              $atts
          );
          $group = trim( $options[\'group\'] );
          $current_group = Groups_Group::read( $group );
          if ( !$current_group ) {
              $current_group = Groups_Group::read_by_name( $group );
          }
          if ( $current_group ) {
              switch( $options[\'show\'] ) {
                  case \'name\' :
                      $output .= wp_filter_nohtml_kses( $current_group->name );
                      break;
                  case \'description\' :
                      $output .= wp_filter_nohtml_kses( $current_group->description );
                      break;
                  case \'count\' :
                      $user_group_table = _groups_get_tablename( "user_group" );
                      $count = $wpdb->get_var( $wpdb->prepare(
                          "SELECT COUNT(*) FROM $user_group_table WHERE group_id = %d",
                          Groups_Utility::id( $current_group->group_id )
                      ) );
                      if ( $count === null ) {
                          $count = 0;
                      } else {
                          $count = intval( $count );
                      }
                      $output .= _n( $options[\'single\'], sprintf( $options[\'plural\'], $count ), $count, GROUPS_PLUGIN_DOMAIN );
                      break;
                  // @todo experimental - could use pagination, sorting, link to profile, ...
                  case \'users\' :
                      $user_group_table = _groups_get_tablename( "user_group" );
                      $users = $wpdb->get_results( $wpdb->prepare(
                          "SELECT * FROM $wpdb->users LEFT JOIN $user_group_table ON $wpdb->users.ID = $user_group_table.user_id WHERE $user_group_table.group_id = %d",
                          Groups_Utility::id( $current_group->group_id )
                      ) );
                      if ( $users ) {
                          $output .= \'<ul>\';
                          foreach( $users as $user ) {
                              $output .= \'<li>\' . wp_filter_nohtml_kses( $user->user_login ) . \'</li>\';
                          }
                          $output .= \'</ul>\';
                      }
      
                      break;
              }
          }
          return $output;
      }
      

    1 个回复
    最合适的回答,由SO网友:Iurie 整理而成

    下面是修改后的函数(比原来短24行),正如评论员所建议的(非常感谢!)。只有当Groups 插件已安装并激活。

    add_shortcode( \'groups_group_info\', \'groups_group_info\' );
    
    function groups_group_info( $atts, $content = null ) {
    global $wpdb;
    $output = "";
    $options = shortcode_atts(
        array(
            \'group\' => \'\',
            \'show\' => \'\',
            \'format\' => \'\',
            \'single\' => \'1\',
            \'plural\' => \'%d\'
        ),
        $atts
    );
    $group = trim( $options[\'group\'] );
    $current_group = Groups_Group::read( $group );
    if ( !$current_group ) {
        $current_group = Groups_Group::read_by_name( $group );
    }
    if ( $current_group ) {
                $user_group_table = _groups_get_tablename( "user_group" );
                $users = $wpdb->get_results( $wpdb->prepare(
                    "SELECT * FROM $wpdb->users LEFT JOIN $user_group_table ON $wpdb->users.ID = $user_group_table.user_id WHERE $user_group_table.group_id = %d",
                    Groups_Utility::id( $current_group->group_id )
                ) );
                if ( $users ) {
                    $output .= \'<ul>\';
                    foreach( $users as $user ) {
                        $output .= \'<li>\' . wp_filter_nohtml_kses( $user->display_name ) . \'</li>\';
                    }
                    $output .= \'</ul>\';
                }
    }
    return $output;
    }
    

    结束

    相关推荐

    Using shortcodes in PHP

    我试图在我的页面中使用短代码,我从一个简单的测试开始。在里面functions.php:function HelloWorldShortcode() { return \'<p>Hello World!</p>\'; } add_shortcode(\'helloworld\', \'HelloWorldShortcode\'); 在中index.php:[helloworld] 这不会产生<p>Hello World