admin管理员组文章数量:1406052
So wp_get_themes() returns an array of objects :
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] =>
[Description] => blak blak.
[Author] => Joy division
[AuthorURI] =>
[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] =>
[Description] => Blak Blak.
[Author] => Some dude
[AuthorURI] =>
[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
)
)
I'm trying to build function that returns an array of the theme names from the objects.
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;
}
However, this is just returning an empty array. What am I missing?
So wp_get_themes() returns an array of objects :
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
[Description] => blak blak.
[Author] => Joy division
[AuthorURI] => http://www.someurl
[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
[Description] => Blak Blak.
[Author] => Some dude
[AuthorURI] => http://www.someurl
[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
)
)
I'm trying to build function that returns an array of the theme names from the objects.
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;
}
However, this is just returning an empty array. What am I missing?
Share Improve this question asked Jul 26, 2016 at 20:41 friendlyfirefriendlyfire 1051 silver badge11 bronze badges 3 |3 Answers
Reset to default 3Correct the wp_get_themes()
function has most of the information inaccessible to the public which requires you to pull the info out using the $theme->get( 'Name' );
format. You can build a simple array like so.
// 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 );
Which will output
Array (
[0] => Anchor Blank
[1] => Swell - Anchor Hosting
[2] => Swell
)
Taking this one step future you can build an array with all of the theme data like so.
// 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 );
This will output an array which looks like the following
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
[AuthorURI] => http://themetrust
[Version] => 1.2.6
[Template] =>
[Status] => publish
[Tags] => Array
(
)
[TextDomain] => swell
[DomainPath] => /languages/
)
)
you can access WP_Theme
object like below
$theme = wp_get_theme();
$name = $theme->{'Name'};
or
$theme->get( 'Name' );
Here theme names that are displayed from wp_get_themes()
stores private information, as you can see
[headers:WP_Theme:private]
So you can't access the private variable directly. For accessing the private variable you need to inherit the parent class.
Update
$get_theme = wp_get_themes();
$all_theme = array_keys( $get_theme ) );
print_r( $all_theme ); //will return all theme
本文标签: phpHow to loop over wpgetthemes() and create an array of themes name
版权声明:本文标题:php - How to loop over wp_get_themes() and create an array of themes name 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744938481a2633334.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$allowed_theme->name
, the__get()
method will return the proper value. – fuxia ♦ Commented Jul 26, 2016 at 21:03