我有一个自定义WordPress帖子数组,如下所示:
Array (
[1] => Array (
[sno] => 1
[post] => WP_Post Object (
[ID] => 1452
[post_author] => 12
[post_date] => 2017-06-04 18:09:47
[post_date_gmt] => 2017-06-05 01:09:47
[post_content] => Content here
[post_title] => Title here
[post_excerpt] =>
[post_status] => publish
[comment_status] => open
[ping_status] => closed
[post_password] =>
[post_name] => title-here
[to_ping] =>
[pinged] =>
[post_modified] => 2017-07-14 09:35:35
[post_modified_gmt] => 2017-07-14 16:35:35
[post_content_filtered] =>
[post_parent] => 0
[guid] => https://urlhere.com&p=1452
[menu_order] => 0
[post_type] => sfwd-lessons
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[permalink] => https://urlhere.com/
[sub_title] =>
[status] => notcompleted
[sample] => is_not_sample
[lesson_access_from] =>
)
)
我正试图用这种逻辑循环遍历这个数组中的帖子。如果“status”等于“completed”,则输出文章标题和链接。我认为基本的foreach循环应该可以工作,但它似乎不起作用。阵列的创建方式如下:
$lessons = learndash_get_course_lessons_list( $course_id );
我的foreach循环如下所示:
foreach ($lessons as $key => $lesson) {
echo $lesson["post_title"];
}
但这不会返回任何结果。我需要做什么来循环遍历数组数据?
最合适的回答,由SO网友:Johansson 整理而成
根据您所说的关于帖子完成的内容,您可以检查其状态是否为completed
如果是,请指向标题:
foreach ( $lessons as $lesson ){
if( $lesson[\'status\'] == \'completed\' ){
echo $lesson[\'post\']->post_title;
}
}