函数文档

wp_get_themes()

💡 云策文档标注

概述

wp_get_themes() 函数根据参数返回一个 WP_Theme 对象数组,用于检索主题信息。相比已弃用的 get_themes() 有所改进,但性能开销较大,建议优先使用 wp_get_theme()。

关键要点

  • 返回 WP_Theme 对象数组,支持通过参数过滤主题列表。
  • 参数包括 errors(控制是否返回有错误的主题)、allowed(在多站点环境中控制允许的主题类型)和 blog_id(指定博客ID)。
  • 函数内部处理多站点主题权限和错误过滤,并缓存 WP_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 ) {
        /*
         * 确保活动主题优先,以防 search_theme_directories() 在冲突时选择错误主题根。
         */
        $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 ( (bool) $wp_theme->errors() !== $args['errors'] ) {
                unset( $themes[ $theme ] );
            }
        }
    }

    return $themes;
}

注意事项

  • 函数自 WordPress 3.4.0 版本引入,替代了已弃用的 get_themes()。
  • 在多站点环境中,allowed 参数可设置为 'network'、'site' 或布尔值,以过滤网络或站点允许的主题。
  • errors 参数为 true 时返回有错误的主题,false 时返回无错误的主题,null 时返回所有主题。
  • 由于性能考虑,在主题数量较多时应避免频繁调用此函数。

📄 原文内容

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.

Parameters

$argsarrayoptional
The search arguments.

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

Default:array()

Return

WP_Theme[] Array of WP_Theme objects.

Source

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 ( (bool) $wp_theme->errors() !== $args['errors'] ) {
				unset( $themes[ $theme ] );
			}
		}
	}

	return $themes;
}

Changelog

Version Description
3.4.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example:

    </pre>
    <p>The result will be:</p>
    <pre class="wp-block-code"><code lang="php" class="language-php line-numbers">["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) "<a href="https://wordpress.org/themes/twentyseventeen/&quot" rel="nofollow ugc">https://wordpress.org/themes/twentyseventeen/"</a>;
          ["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) "<a href="https://wordpress.org/&quot" rel="nofollow ugc">https://wordpress.org/"</a>;
          ["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) ""
      }