wp_get_themes

函数
wp_get_themes ( $args = array() )
参数
  • (array) $args { Optional. The search arguments. @type mixed $errors True to return themes with errors, false to return themes without errors, null to return all themes. Default false. @type mixed $allowed (Multisite) True to return only allowed themes for a site. False to return only disallowed themes for a site. 'site' to return only site-allowed themes. 'network' to return only network-allowed themes. Null to return all themes. Default null. @type int $blog_id (Multisite) The blog ID used to calculate which themes are allowed. Default 0, synonymous for the current blog. }
    Required:
    Default: array()
返回值
  • (WP_Theme[]) Array of WP_Theme objects.
定义位置
相关方法
wp_get_themeget_themesget_themewp_get_siteswp_using_themes
引入
3.4.0
弃用
-

wp_get_themes: 这个函数检索一个已安装的主题数组。它返回一个WP_Theme对象的数组,代表每个已安装的主题。WP_Theme对象提供了关于该主题的信息,例如名称、版本、作者、描述和截图URL。

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

尽管比get_themes()有进步,但这个函数还是相当昂贵的,而且会随着主题的增加而线性增长。如果可能的话,请坚持使用wp_get_theme()。

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 active 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, true ) ) {
				$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 ] = new WP_Theme( $theme, $theme_root['theme_root'] );

			$_themes[ $theme_root['theme_root'] . '/' . $theme ] = $themes[ $theme ];
		}
	}

	if ( null !== $args['errors'] ) {
		foreach ( $themes as $theme => $wp_theme ) {
			if ( $wp_theme->errors() != $args['errors'] ) {
				unset( $themes[ $theme ] );
			}
		}
	}

	return $themes;
}

常见问题

FAQs
查看更多 >