返回介绍

wp_get_themes()

发布于 2017-09-11 12:09:29 字数 8515 浏览 1116 评论 0 收藏 0

wp_get_themes( array $args = array() )

Returns an array of WP_Theme objects based on the arguments.


description

Despite advances over get_themes(), this function is quite expensive, and grows linearly with additional themes. Stick to wp_get_theme() if possible.


参数

$args

(array) (Optional) The search arguments. Optional. - errors mixed True to return themes with errors, false to return themes without errors, null to return all themes. Defaults to false. - allowed mixed (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. Defaults to null. - blog_id int (Multisite) The blog ID used to calculate which themes are allowed. Defaults to 0, synonymous for the current blog.

Default value: array()


返回值

(array) Array of WP_Theme objects.


源代码

File: wp-includes/theme.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 ( 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;
}

更新日志

Versiondescription
3.4.0Introduced.

相关函数

Uses

  • wp-includes/theme.php: get_raw_theme_root()
  • wp-includes/theme.php: search_theme_directories()
  • wp-includes/theme.php: get_stylesheet()
  • wp-includes/class-wp-theme.php: WP_Theme::get_allowed_on_network()
  • wp-includes/class-wp-theme.php: WP_Theme::get_allowed_on_site()
  • wp-includes/class-wp-theme.php: WP_Theme::get_allowed()
  • wp-includes/class-wp-theme.php: WP_Theme::errors()
  • wp-includes/class-wp-theme.php: WP_Theme::__construct()
  • wp-includes/load.php: is_multisite()
  • wp-includes/functions.php: wp_parse_args()
  • Show 5 more uses Hide more uses

Used By

  • wp-admin/includes/theme.php: wp_prepare_themes_for_js()
  • wp-admin/includes/deprecated.php: get_allowed_themes()
  • wp-admin/includes/deprecated.php: get_broken_themes()
  • wp-admin/includes/class-wp-ms-themes-list-table.php: WP_MS_Themes_List_Table::prepare_items()
  • wp-admin/includes/dashboard.php: wp_welcome_panel()
  • wp-admin/includes/upgrade.php: upgrade_network()
  • wp-admin/includes/class-wp-themes-list-table.php: WP_Themes_List_Table::prepare_items()
  • wp-includes/theme.php: wp_clean_themes_cache()
  • wp-includes/deprecated.php: get_themes()
  • wp-includes/class-wp-theme.php: WP_Theme::get_allowed_on_site()
  • wp-includes/update.php: wp_update_themes()
  • Show 6 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by vee

    Example:

    <?php
    $all_themes = wp_get_themes();
    
    print_r($all_themes);

    The result will be:

    
    ["twentyseventeen"]=>
      object(WP_Theme)#1365 (12) {
        ["update"]=>
        bool(false)
        ["theme_root":"WP_Theme":private]=>
        string(59) "/path/to/site/wp-content/themes"
        ["headers":"WP_Theme":private]=>
        array(11) {
          ["Name"]=>
          string(16) "Twenty Seventeen"
          ["ThemeURI"]=>
          string(45) "https://wordpress.org/themes/twentyseventeen/"
          ["description"]=>
          string(450) "Twenty Seventeen brings your site to life with header video and immersive featured images. With a focus on business sites, it features multiple sections on the front page as well as widgets, navigation and social menus, a logo, and more. Personalize its asymmetrical grid with a custom color scheme and showcase your multimedia content with post formats. Our default theme for 2017 works great in many languages, for any abilities, and on any device."
          ["Author"]=>
          string(18) "the WordPress team"
          ["AuthorURI"]=>
          string(22) "https://wordpress.org/"
          ["Version"]=>
          string(3) "1.3"
          ["Template"]=>
          string(0) ""
          ["Status"]=>
          string(0) ""
          ["Tags"]=>
          string(281) "one-column, two-columns, right-sidebar, flexible-header, accessibility-ready, custom-colors, custom-header, custom-menu, custom-logo, editor-style, featured-images, footer-widgets, post-formats, rtl-language-support, sticky-post, theme-options, threaded-comments, translation-ready"
          ["TextDomain"]=>
          string(15) "twentyseventeen"
          ["DomainPath"]=>
          string(0) ""
        }
        ["headers_sanitized":"WP_Theme":private]=>
        NULL
        ["name_translated":"WP_Theme":private]=>
        NULL
        ["errors":"WP_Theme":private]=>
        NULL
        ["stylesheet":"WP_Theme":private]=>
        string(15) "twentyseventeen"
        ["template":"WP_Theme":private]=>
        string(15) "twentyseventeen"
        ["parent":"WP_Theme":private]=>
        NULL
        ["theme_root_uri":"WP_Theme":private]=>
        NULL
        ["textdomain_loaded":"WP_Theme":private]=>
        NULL
        ["cache_hash":"WP_Theme":private]=>
        string(32) ""
      }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文