Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在wordpress php中迭代数组_Php_Arrays_Wordpress - Fatal编程技术网

在wordpress php中迭代数组

在wordpress php中迭代数组,php,arrays,wordpress,Php,Arrays,Wordpress,我试图用'foreach'遍历这个数组,以获得[post_title]。但不幸的是,它没有在我的浏览器中打印任何内容。有人能解释一下我可以遍历这个数组吗 Array ( [0] => WP_Post Object ( [ID] => 6 [post_author] => 1 [post_date] => 2017-11-09 12:56:27 [post_date_gmt] => 2017-11

我试图用'foreach'遍历这个数组,以获得[post_title]。但不幸的是,它没有在我的浏览器中打印任何内容。有人能解释一下我可以遍历这个数组吗

Array
(
[0] => WP_Post Object
    (
        [ID] => 6
        [post_author] => 1
        [post_date] => 2017-11-09 12:56:27
        [post_date_gmt] => 2017-11-09 12:56:27
        [post_content] => 
        [post_title] => Hi everybody
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] => 
        [post_name] => hi-everybody
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2017-11-09 12:56:27
        [post_modified_gmt] => 2017-11-09 12:56:27
        [post_content_filtered] => 
        [post_parent] => 0
        [menu_order] => 0
        [post_type] => post
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )

[1] => WP_Post Object
    (
        [ID] => 1
        [post_author] => 1
        [post_date] => 2017-11-06 22:35:17
        [post_date_gmt] => 2017-11-06 22:35:17
        [post_content] => Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
        [post_title] => Hello world!
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] => 
        [post_name] => hello-world
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2017-11-06 22:35:17
        [post_modified_gmt] => 2017-11-06 22:35:17
        [post_content_filtered] => 
        [post_parent] => 0
        [menu_order] => 0
        [post_type] => post
        [post_mime_type] => 
        [comment_count] => 1
        [filter] => raw
    )

)
以下是我的foreach循环代码:

    $my_posts = new WP_Query;
    $myposts = $my_posts->query( array('post_type' => 'post'));

            foreach ($post as $myposts ) {

                        echo $post['post_title'];
                 }             
}

同样,我需要得到post_title的值,但没有得到输出。非常感谢您的帮助。

您的数组中有一个对象。所以你需要做一些类似的事情

foreach ($array[WP_Post] as $item) {
  echo $item;
}
或者,到目前为止您尝试了什么?

尝试以下方法:

$my_posts = new WP_Query; 
$myposts = $my_posts->query( array('post_type' => 'post')); 
foreach ($post as $myposts ) { 
    echo $myposts->post_title; 
} 

您的代码应该如下所示:

$my_posts = new WP_Query;
$myposts = $my_posts->query( array('post_type' => 'post'));

foreach ($myposts  as $post )
{
    echo $post->post_title;
}

请发布你的
foreach
循环代码。这是我的foreach循环代码:$my\u posts=new WP\u Query$myposts=$my_posts->query(数组('post_type'=>'post');foreach($post as$myposts){echo$post['post_title'];}}}好的@sem horse,检查您得到的这些答案。我希望你能找到解决办法。