【问题标题】:Get array value in $wp_query->posts var_dump在 $wp_query->posts var_dump 中获取数组值
【发布时间】:2017-03-23 23:37:47
【问题描述】:

我有以下代码:

$posts = $wp_query->posts;
foreach($posts as $post){
    var_dump($post);
}

这将获取正在运行的查询的所有帖子并显示所有帖子信息。 var_dump 看起来像这样:

object(WP_Post)#84 (24) { 
    ["ID"]=> int(1190) 
    ["post_author"]=> string(1) "1" 
    ["post_date"]=> string(19) "2016-10-20 14:17:55" 
    ["post_date_gmt"]=> string(19) "2016-10-20 03:17:55" 
    ["post_content"]=> string(0) "" 
    ["post_title"]=> string(21) "Chilled water systems" 
    ["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(21) "chilled-water-systems" 
    ["to_ping"]=> string(0) "" 
    ["pinged"]=> string(0) "" 
    ["post_modified"]=> string(19) "2016-10-21 11:23:49"
    ["post_modified_gmt"]=> string(19) "2016-10-21 00:23:49" 
    ["post_content_filtered"]=> string(0) "" 
    ["post_parent"]=> int(1182) 
    ["guid"]=> string(48) "http://siteurl.com" 
    ["menu_order"]=> int(0) **
    ["post_type"]=> string(4) "page"** 
    ["post_mime_type"]=> string(0) "" 
    ["comment_count"]=> string(1) "0" 
    ["filter"]=> string(3) "raw" 
}

我想知道如何从 var_dump 中获取 post_type 值。 (粗体字)我试过了:

print_r($post['post_type']);
print_r($post[0]['post_type']);

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    试试这个:

    echo $post->post_type
    

    更多细节:

    如果订购输出,你可以看到:

    object(WP_Post)#84 (24)
    {
    ["ID"]=> int(1190) 
    ["post_author"]=> string(1) "1" 
    ["post_date"]=> string(19) "2016-10-20 14:17:55" 
    ["post_date_gmt"]=> string(19) "2016-10-20 03:17:55" 
    ["post_content"]=> string(0) "" 
    ["post_title"]=> string(21) "Chilled water systems" 
    ["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(21) "chilled-water-systems" 
    ["to_ping"]=> string(0) "" 
    ["pinged"]=> string(0) "" 
    ["post_modified"]=> string(19) "2016-10-21 11:23:49" 
    ["post_modified_gmt"]=> string(19) "2016-10-21 00:23:49" 
    ["post_content_filtered"]=> string(0) "" 
    ["post_parent"]=> int(1182) 
    ["guid"]=> string(48) "http://siteurl.com" 
    ["menu_order"]=> int(0) 
    ["post_type"]=> string(4) "page" 
    ["post_mime_type"]=> string(0) "" 
    ["comment_count"]=> string(1) "0" 
    ["filter"]=> string(3) "raw"
    }
    

    您有一个对象,并且要访问对象的字段,您必须使用->,因此,您可以使用$post->post_type 作为帖子类型目标。

    完整代码:

    $posts = $wp_query->posts;
    foreach($posts as $post){
        var_dump($post->post_type);
    }
    

    【讨论】:

    • 像魅力一样工作
    • 还有一件事。变量 $posts 将在数组中有多个对象。我将如何获得一个,例如,如果它是一个普通数组,您将使用 $posts[0]
    • @AJDEV 你可以使用这个:$posts[0]->post_type
    猜你喜欢
    • 2014-05-13
    • 2014-06-20
    • 1970-01-01
    • 2021-10-25
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多