如何循环遍历wp_GET_Themes()并创建主题名称数组

时间:2016-07-26 作者:friendlyfire

因此wp\\u get\\u themes()返回一个对象数组:

Array
(
    [WpAngular] => WP_Theme Object
        (
            [update] => 
            [theme_root:WP_Theme:private] => /home/love/Web/web/app/themes
            [headers:WP_Theme:private] => Array
                (
                    [Name] => WpAngular
                    [ThemeURI] => http://www.someurl.com
                    [Description] => blak blak.
                    [Author] => Joy division
                    [AuthorURI] => http://www.someurl.com
                    [Version] => 6.0
                    [Template] => 
                    [Status] => 
                    [Tags] => WordPress
                    [TextDomain] => 
                    [DomainPath] => 
                )

            [headers_sanitized:WP_Theme:private] => Array
                (
                    [Name] => WpAngular
                )

            [name_translated:WP_Theme:private] => 
            [errors:WP_Theme:private] => 
            [stylesheet:WP_Theme:private] => Angular-Wordpress
            [template:WP_Theme:private] => Angular-Wordpress
            [parent:WP_Theme:private] => 
            [theme_root_uri:WP_Theme:private] => 
            [textdomain_loaded:WP_Theme:private] => 
            [cache_hash:WP_Theme:private] => 03dc86f794762ab23bab120e9b121326
        )

    [Some Theme] => WP_Theme Object
        (
            [update] => 
            [theme_root:WP_Theme:private] => /home/love6/Web/web/app/themes
            [headers:WP_Theme:private] => Array
                (
                    [Name] => Some Theme
                    [ThemeURI] => http://www.someurl.com
                    [Description] => Blak Blak.
                    [Author] => Some dude
                    [AuthorURI] => http://www.someurl.com
                    [Version] => 2.0
                    [Template] => 
                    [Status] => 
                    [Tags] => 
                    [TextDomain] => 
                    [DomainPath] => 
                )

            [headers_sanitized:WP_Theme:private] => Array
                (
                    [Name] => Some Theme
                )

            [name_translated:WP_Theme:private] => 
            [errors:WP_Theme:private] => 
            [stylesheet:WP_Theme:private] => some-theme
            [template:WP_Theme:private] => some-theme
            [parent:WP_Theme:private] => 
            [theme_root_uri:WP_Theme:private] => 
            [textdomain_loaded:WP_Theme:private] => 
            [cache_hash:WP_Theme:private] => a1a2613543a81b28b06ac802adb785fc
        )
)
我试图构建一个函数,从对象返回一个主题名称数组。

function my_get_allowed_themes() {



        $theme_args = array( \'errors\' => false , \'allowed\' => \'site\' );

        $allowed_themes = wp_get_themes($theme_args);

        $allowed_themes = null;

        $demos = array();

        $allowed_theme_names = array();

         // loop over themes and grab the theme name
         foreach ( $allowed_themes as $allowed_theme ) {

               $allowed_theme_names[] = $allowed_theme[\'Name\'];


          }

          return $allowed_theme_names;    

    }
然而,这只是返回一个空数组。我错过了什么?

3 个回复
SO网友:Austin Ginder

更正wp_get_themes() 函数具有大多数公众无法访问的信息,这要求您使用$theme->get( \'Name\' ); 总体安排您可以这样构建一个简单的数组。

// Build new empty Array to store the themes
$themes = array();

// Loads theme data
$all_themes = wp_get_themes();

// Loads theme names into themes array
foreach ($all_themes as $theme) {
  $themes[] = $theme->get(\'Name\');
}

// Prints the theme names
print_r( $themes );
将输出

Array (
  [0] => Anchor Blank
  [1] => Swell - Anchor Hosting
  [2] => Swell
)
通过这一步,您可以构建一个包含所有主题数据的数组,如下所示。

// Build new empty Array to store the themes
$themes = array();

// Loads theme data
$all_themes = wp_get_themes();

// Build theme data manually
foreach ($all_themes as $theme) {
  $themes{ $theme->stylesheet } = array(
    \'Name\' => $theme->get(\'Name\'),
    \'Description\' => $theme->get(\'Description\'),
    \'Author\' => $theme->get(\'Author\'),
    \'AuthorURI\' => $theme->get(\'AuthorURI\'),
    \'Version\' => $theme->get(\'Version\'),
    \'Template\' => $theme->get(\'Template\'),
    \'Status\' => $theme->get(\'Status\'),
    \'Tags\' => $theme->get(\'Tags\'),
    \'TextDomain\' => $theme->get(\'TextDomain\'),
    \'DomainPath\' => $theme->get(\'DomainPath\')
  );
}

// Prints the themes
print_r( $themes );
这将输出一个如下所示的数组

Array (
[anchor-blank] => Array
    (
        [Name] => Anchor Blank
        [Description] => Anchor Hosting blank theme
        [Author] => Anchor Hosting
        [AuthorURI] => https://anchor.host
        [Version] => 1.1
        [Template] =>
        [Status] => publish
        [Tags] => Array
            (
            )

        [TextDomain] => anchor-blank
        [DomainPath] => /languages/
    )

[swell-anchorhost] => Array
    (
        [Name] => Swell - Anchor Hosting
        [Description] => Child theme for Anchor Hosting
        [Author] => Anchor Hosting
        [AuthorURI] => https://anchor.host
        [Version] => 1.1.0
        [Template] => swell
        [Status] => publish
        [Tags] => Array
            (
            )

        [TextDomain] => swell
        [DomainPath] => /languages/
    )

[swell] => Array
    (
        [Name] => Swell
        [Description] => Swell is a one-column, typography-focused, video Wordpress theme.
        [Author] => ThemeTrust.com
        [AuthorURI] => http://themetrust.com
        [Version] => 1.2.6
        [Template] =>
        [Status] => publish
        [Tags] => Array
            (
            )

        [TextDomain] => swell
        [DomainPath] => /languages/
    )
)

SO网友:Thamaraiselvam

您可以访问WP_Theme 如下所示的对象

 $theme = wp_get_theme();
 $name = $theme->{\'Name\'};

$theme->get( \'Name\' );

SO网友:Aftab

此处显示的主题名称wp_get_themes() 存储私人信息,如您所见

[headers:WP_Theme:private]

因此不能直接访问私有变量。要访问私有变量,需要继承父类。

更新

$get_theme = wp_get_themes();
$all_theme = array_keys( $get_theme ) );
print_r( $all_theme );  //will return all theme