函数文档

wp_credits_section_list()

💡 云策文档标注

概述

wp_credits_section_list() 函数用于显示指定贡献者组的列表,支持不同类型(如列表、库、紧凑视图)的渲染方式。它处理来自 API 的贡献者数据,并根据组类型输出格式化内容。

关键要点

  • 函数接受两个参数:$credits(可选,API 返回的贡献者组数据,默认空数组)和 $slug(必需,要显示的当前组标识符)。
  • 根据 $group_data['type'] 的值,函数使用 switch 语句处理三种情况:'list' 类型添加个人资料链接并输出列表,'libraries' 类型构建对象链接并输出,默认情况处理紧凑或标准视图,显示头像和名称。
  • 函数内部使用 shuffle() 随机化数据(如果组数据指定了 'shuffle'),并调用辅助函数如 _wp_credits_add_profile_link 和 _wp_credits_build_object_link 进行数据处理。
  • 输出时使用 esc_html() 等函数确保安全性,并支持多语言翻译(通过 translate())。

代码示例

function wp_credits_section_list( $credits = array(), $slug = '' ) {
    $group_data   = isset( $credits['groups'][ $slug ] ) ? $credits['groups'][ $slug ] : array();
    $credits_data = $credits['data'];
    if ( ! count( $group_data ) ) {
        return;
    }

    if ( ! empty( $group_data['shuffle'] ) ) {
        shuffle( $group_data['data'] );
    }

    switch ( $group_data['type'] ) {
        case 'list':
            array_walk( $group_data['data'], '_wp_credits_add_profile_link', $credits_data['profiles'] );
            echo '' . wp_sprintf( '%l.', $group_data['data'] ) . "nn";
            break;
        case 'libraries':
            array_walk( $group_data['data'], '_wp_credits_build_object_link' );
            echo '' . wp_sprintf( '%l.', $group_data['data'] ) . "nn";
            break;
        default:
            $compact = 'compact' === $group_data['type'];
            $classes = 'wp-people-group ' . ( $compact ? 'compact' : '' );
            echo '' . "n";
            foreach ( $group_data['data'] as $person_data ) {
                echo '' . "nt";
                echo '';
                $size   = $compact ? 80 : 160;
                $data   = get_avatar_data( $person_data[1] . '@sha256.gravatar.com', array( 'size' => $size ) );
                $data2x = get_avatar_data( $person_data[1] . '@sha256.gravatar.com', array( 'size' => $size * 2 ) );
                echo '' . "n";
                echo esc_html( $person_data[0] ) . "nt";
                if ( ! $compact && ! empty( $person_data[3] ) ) {
                    echo '' . translate( $person_data[3] ) . "n";
                }
                echo "n";
            }
            echo "n";
            break;
    }
}

注意事项

  • 函数在 WordPress 5.3.0 版本中引入,使用时需确保版本兼容性。
  • 参数 $credits 应包含从 API 获取的结构化数据,否则可能无法正确显示贡献者列表。
  • 输出内容已进行转义处理(如 esc_html),但开发者仍需注意输入数据的来源安全性。
  • 相关函数如 get_avatar_data、translate、wp_sprintf 等用于辅助功能,建议熟悉其用法以优化实现。

📄 原文内容

Displays a list of contributors for a given group.

Parameters

$creditsarrayoptional
The credits groups returned from the API.

Default:array()

$slugstringrequired
The current group to display.

Source

function wp_credits_section_list( $credits = array(), $slug = '' ) {
	$group_data   = isset( $credits['groups'][ $slug ] ) ? $credits['groups'][ $slug ] : array();
	$credits_data = $credits['data'];
	if ( ! count( $group_data ) ) {
		return;
	}

	if ( ! empty( $group_data['shuffle'] ) ) {
		shuffle( $group_data['data'] ); // We were going to sort by ability to pronounce "hierarchical," but that wouldn't be fair to Matt.
	}

	switch ( $group_data['type'] ) {
		case 'list':
			array_walk( $group_data['data'], '_wp_credits_add_profile_link', $credits_data['profiles'] );
			echo '<p class="wp-credits-list">' . wp_sprintf( '%l.', $group_data['data'] ) . "</p>nn";
			break;
		case 'libraries':
			array_walk( $group_data['data'], '_wp_credits_build_object_link' );
			echo '<p class="wp-credits-list">' . wp_sprintf( '%l.', $group_data['data'] ) . "</p>nn";
			break;
		default:
			$compact = 'compact' === $group_data['type'];
			$classes = 'wp-people-group ' . ( $compact ? 'compact' : '' );
			echo '<ul class="' . $classes . '" id="wp-people-group-' . $slug . '">' . "n";
			foreach ( $group_data['data'] as $person_data ) {
				echo '<li class="wp-person" id="wp-person-' . esc_attr( $person_data[2] ) . '">' . "nt";
				echo '<a href="' . esc_url( sprintf( $credits_data['profiles'], $person_data[2] ) ) . '" class="web">';
				$size   = $compact ? 80 : 160;
				$data   = get_avatar_data( $person_data[1] . '@sha256.gravatar.com', array( 'size' => $size ) );
				$data2x = get_avatar_data( $person_data[1] . '@sha256.gravatar.com', array( 'size' => $size * 2 ) );
				echo '<span class="wp-person-avatar"><img src="' . esc_url( $data['url'] ) . '" srcset="' . esc_url( $data2x['url'] ) . ' 2x" class="gravatar" alt="" /></span>' . "n";
				echo esc_html( $person_data[0] ) . "</a>nt";
				if ( ! $compact && ! empty( $person_data[3] ) ) {
					// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
					echo '<span class="title">' . translate( $person_data[3] ) . "</span>n";
				}
				echo "</li>n";
			}
			echo "</ul>n";
			break;
	}
}

Changelog

Version Description
5.3.0 Introduced.