get_post_mime_types

函数
get_post_mime_types ( No parameters )
返回值
  • (array) List of post mime types.
定义位置
相关方法
get_post_mime_typeget_post_typesget_post_typeget_available_post_mime_typeswp_get_mime_types
引入
2.9.0
弃用
-

get_post_mime_types函数为post-attachment返回所有注册的mime类型的数组。它不带任何参数。

获取默认的文章MIME类型。

function get_post_mime_types() {
	$post_mime_types = array(   // array( adj, noun )
		'image'       => array(
			__( 'Images' ),
			__( 'Manage Images' ),
			/* translators: %s: Number of images. */
			_n_noop(
				'Image <span class="count">(%s)</span>',
				'Images <span class="count">(%s)</span>'
			),
		),
		'audio'       => array(
			_x( 'Audio', 'file type group' ),
			__( 'Manage Audio' ),
			/* translators: %s: Number of audio files. */
			_n_noop(
				'Audio <span class="count">(%s)</span>',
				'Audio <span class="count">(%s)</span>'
			),
		),
		'video'       => array(
			_x( 'Video', 'file type group' ),
			__( 'Manage Video' ),
			/* translators: %s: Number of video files. */
			_n_noop(
				'Video <span class="count">(%s)</span>',
				'Video <span class="count">(%s)</span>'
			),
		),
		'document'    => array(
			__( 'Documents' ),
			__( 'Manage Documents' ),
			/* translators: %s: Number of documents. */
			_n_noop(
				'Document <span class="count">(%s)</span>',
				'Documents <span class="count">(%s)</span>'
			),
		),
		'spreadsheet' => array(
			__( 'Spreadsheets' ),
			__( 'Manage Spreadsheets' ),
			/* translators: %s: Number of spreadsheets. */
			_n_noop(
				'Spreadsheet <span class="count">(%s)</span>',
				'Spreadsheets <span class="count">(%s)</span>'
			),
		),
		'archive'     => array(
			_x( 'Archives', 'file type group' ),
			__( 'Manage Archives' ),
			/* translators: %s: Number of archives. */
			_n_noop(
				'Archive <span class="count">(%s)</span>',
				'Archives <span class="count">(%s)</span>'
			),
		),
	);

	$ext_types  = wp_get_ext_types();
	$mime_types = wp_get_mime_types();

	foreach ( $post_mime_types as $group => $labels ) {
		if ( in_array( $group, array( 'image', 'audio', 'video' ), true ) ) {
			continue;
		}

		if ( ! isset( $ext_types[ $group ] ) ) {
			unset( $post_mime_types[ $group ] );
			continue;
		}

		$group_mime_types = array();
		foreach ( $ext_types[ $group ] as $extension ) {
			foreach ( $mime_types as $exts => $mime ) {
				if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
					$group_mime_types[] = $mime;
					break;
				}
			}
		}
		$group_mime_types = implode( ',', array_unique( $group_mime_types ) );

		$post_mime_types[ $group_mime_types ] = $labels;
		unset( $post_mime_types[ $group ] );
	}

	/**
	 * Filters the default list of post mime types.
	 *
	 * @since 2.5.0
	 *
	 * @param array $post_mime_types Default list of post mime types.
	 */
	return apply_filters( 'post_mime_types', $post_mime_types );
}

常见问题

FAQs
查看更多 >