循环对象数组(PHP)

时间:2013-07-10 21:23:21

标签: arrays wordpress

我一直试图遍历这个数组,但我根本无法检索帖子标题。它可能是我想念的一些东西,但我无法做到。

Array
(
[0] => WP_Post Object
    (
        [ID] => 5366
        [post_author] => 1
        [post_date] => 2013-07-09 12:06:00
        [post_date_gmt] => 2013-07-09 12:06:00
        [post_content] => 
        [post_title] => Mini Face Lift
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] => 
        [post_name] => mini-face-lift
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2013-07-09 12:06:00
        [post_modified_gmt] => 2013-07-09 12:06:00
        [post_content_filtered] => 
        [post_parent] => 17
    )
    )

如果我想获得帖子标题 - 我该怎么做?因为我被卡住,我真的很感激你的帮助。

提前感谢!

2 个答案:

答案 0 :(得分:11)

它不是一个多维数组,而是一个对象数组。 。 。尝试类似的事情:

$varName[0]->post_title

可替换地:

$varName[0]['post_title']

如果您尝试迭代并获得每个标题,您可能需要以下内容:

foreach ($varName as $key=>$wpPostObject) {
    echo $wpPostObject->post_title;
}

答案 1 :(得分:0)

尝试:

$examplePost = get_post(); //Post object you are retrieving

echo apply_filters( 'single_post_title', $examplePost->post_title ); //echos the post name, use the_content instead, if you are doing this in the loop