根据参数返回WP_Theme对象的数组。

原型

wp_get_themes( array $args = array() )

描述

尽管get_themes()取得了进步,但这个功能非常昂贵,而且随着其他主题线性增长。如果可能的话,坚持使用wp_get_theme()。

参数

$args (array) (Optional) 搜索参数。可选的。 – errors mixed true返回有错误的主题,false返回没有错误的主题,null返回所有主题。默认为false。 – 允许混合(多站点)True仅返回站点允许的主题。如果仅返回网站的不允许主题,则为false。 ‘site’仅返回网站允许的主题。 ‘network’仅返回网络允许的主题。 Null返回所有主题。默认为null。 – blog_id int(多站点)用于计算允许哪些主题的博客ID。默认为0,与当前博客同义。

返回值

(array)  数组

源文件

路径:wp-includes/theme.php

<?php
...
function wp_get_themes( $args = array() ) {
	global $wp_theme_directories;
	$defaults = array( 'errors' => false, 'allowed' => null, 'blog_id' => 0 );
	$args = wp_parse_args( $args, $defaults );
	$theme_directories = search_theme_directories();
	if ( is_array( $wp_theme_directories ) && count( $wp_theme_directories ) > 1 ) {
		// Make sure the current theme wins out, in case search_theme_directories() picks the wrong
		// one in the case of a conflict. (Normally, last registered theme root wins.)
		$current_theme = get_stylesheet();
		if ( isset( $theme_directories[ $current_theme ] ) ) {
			$root_of_current_theme = get_raw_theme_root( $current_theme );
			if ( ! in_array( $root_of_current_theme, $wp_theme_directories ) )
				$root_of_current_theme = WP_CONTENT_DIR . $root_of_current_theme;
			$theme_directories[ $current_theme ]['theme_root'] = $root_of_current_theme;
		}
	}
	if ( empty( $theme_directories ) )
		return array();
	if ( is_multisite() && null !== $args['allowed'] ) {
		$allowed = $args['allowed'];
		if ( 'network' === $allowed )
			$theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_network() );
		elseif ( 'site' === $allowed )
			$theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_site( $args['blog_id'] ) );
		elseif ( $allowed )
			$theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
		else
			$theme_directories = array_diff_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
	}
	$themes = array();
	static $_themes = array();
	foreach ( $theme_directories as $theme => $theme_root ) {
		if ( isset( $_themes[ $theme_root['theme_root'] . '/' . $theme ] ) )
			$themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ];
		else
			$themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ] = new WP_Theme( $theme, $theme_root['theme_root'] );
	}
	if ( null !== $args['errors'] ) {
		foreach ( $themes as $theme => $wp_theme ) {
			if ( $wp_theme->errors() != $args['errors'] )
				unset( $themes[ $theme ] );
		}
	}
	return $themes;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_get_themes/