PHP如何遍历WP对象

时间:2016-11-30 11:25:18

标签: php arrays wordpress

我是PHP新手。从我学到/读的东西。要遍历php中的对象,请使用->并遍历数组["itemInArray"]。这可能听起来很愚蠢,但是如何从下面的这个对象中抓取一些东西?比方说,post_content

我已经在一个循环通过多维数组的foreach循环中。看起来像:

foreach ($key as $b=>$test) {
 var_dump($test["post_content"]);
}

-

我的对象:

object(WP_Post)#7239 (24) {
  ["ID"]=>
  int(2127)
  ["post_author"]=>
  string(1) "5"
  ["post_date"]=>
  string(19) "2016-04-29 09:45:55"
  ["post_date_gmt"]=>
  string(19) "2016-04-29 09:45:55"
  ["post_content"]=>
  string(313) "<p class="p1"><span class="s1">Some dummy content</span></p>
<p class="p1"><em>Support from whateves</em></p>"
  ["post_title"]=>
  string(12) "The Title"
  ["post_excerpt"]=>
  string(0) ""
  ["post_status"]=>
  string(7) "publish"
  ["comment_status"]=>
  string(6) "closed"
  ["ping_status"]=>
  string(6) "closed"
  ["post_password"]=>
  string(0) ""
  ["post_name"]=>
  string(12) "the-title"
  ["to_ping"]=>
  string(0) ""
  ["pinged"]=>
  string(0) ""
  ["post_modified"]=>
  string(19) "2016-11-24 14:34:01"
  ["post_modified_gmt"]=>
  string(19) "2016-11-24 14:34:01"
  ["post_content_filtered"]=>
  string(0) ""
  ["post_parent"]=>
  int(0)
  ["guid"]=>
  string(66) "http://www.localhost:8080/?post_type=creator&#038;p=2127"
  ["menu_order"]=>
  int(0)
  ["post_type"]=>
  string(17) "creator"
  ["post_mime_type"]=>
  string(0) ""
  ["comment_count"]=>
  string(1) "0"
  ["filter"]=>
  string(3) "raw"
}

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题。

echo $test->post_content;

PHP数组和对象之间的区别是:

阵列:

$array = new Array();
$array['test'] = 'test';
echo $array['test'];

对象:

$object = new stdClass();
$object->test = 'test';
echo $object->test;