【问题标题】:Why is var_dump displaying NULL when accessing a key that has an array as a value?为什么 var_dump 在访问具有数组作为值的键时显示 NULL?
【发布时间】:2019-05-27 14:45:21
【问题描述】:

我正在尝试循环浏览已安装的 Wordpress 主题以提取一段数据。我需要访问 $value->headers["TextDomain"] 但是当我尝试访问 $value->headers 时,我得到了 NULL 响应。

这是我试图从中提取数据的 Wordpress 主题对象数组:

object(WP_Theme)#795 (12) {
  ["update"]=>
  bool(false)
  ["theme_root":"WP_Theme":private]=>
  string(56) "/Users/tygoss/Projects/QuickPress/prod/wp-content/themes"
  ["headers":"WP_Theme":private]=>
  array(11) {
    ["Name"]=>
    string(10) "Black Jane"
    ["ThemeURI"]=>
    string(0) ""
    ["Description"]=>
    string(187) "Black Jane is a clean SuevaFree child theme with an optional slideshow on homepage, a new header layout in addition to the five header layouts available on SuevaFree and new Google Fonts."
    ["Author"]=>
    string(16) "ThemeinProgress."
    ["AuthorURI"]=>
    string(31) "https://www.themeinprogress.com"
    ["Version"]=>
    string(5) "1.0.4"
    ["Template"]=>
    string(9) "suevafree"
    ["Status"]=>
    string(0) ""
    ["Tags"]=>
    string(302) "blog, e-commerce, photography, custom-background, custom-colors, custom-header, custom-logo, custom-menu, featured-images, flexible-header, footer-widgets, post-formats, right-sidebar, sticky-post, theme-options, threaded-comments, translation-ready, one-column, two-columns, three-columns, grid-layout"
    ["TextDomain"]=>
    string(10) "black-jane"
    ["DomainPath"]=>
    string(10) "/languages"
  }
  ["name_translated":"WP_Theme":private]=>
  NULL
  ["errors":"WP_Theme":private]=>
  NULL
  ["stylesheet":"WP_Theme":private]=>
  string(10) "black-jane"
  ["template":"WP_Theme":private]=>
  string(9) "suevafree"
}

如果我 var_dump( $value->stylesheet ) 我得到 string(10) "black-jane"

但是如果我 var_dump ( $value->headers ) 我得到NULL

这是我的循环:

    $all_themes = wp_get_themes();
    foreach ($all_themes as $key => $value) {
      echo '<pre>';
      var_dump($value->headers);
      echo '</pre>';
    }

$value->headers 不应该转储一个包含 11 个项目的数组吗?为什么它返回 NULL 以及为什么 $value->stylesheet 有效但 $value-headers 无效?

【问题讨论】:

  • 我不确定,但看起来WP_Theme 类中实现了__get() 魔术方法,该方法具有未定义标头属性的switch语句,默认情况是返回null,您可以尝试使用 get( $header ) 方法来访问这些属性.. 所以尝试使用 var_dump($value-&gt;get("TextDomain")); src : core.trac.wordpress.org/browser/tags/5.1.1/src/wp-includes/… 。有一种方法可以返回所有标题并没有接缝,但我可能没有充分挖掘
  • @Frankich 成功了!我的最终代码是:$all_themes = wp_get_themes(); $theme_slugs = array(); foreach ($all_themes as $key =&gt; $value) { array_push($theme_slugs, $value-&gt;get("TextDomain")); }

标签: php arrays wordpress themes


【解决方案1】:

$value-&gt;get("the thing from the header array") 完美运行!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-28
    • 2017-03-04
    • 2016-07-05
    • 2018-10-17
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多