循环访问WP_Post对象

时间:2017-07-17 作者:Andrew

我有一个自定义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"];
}
但这不会返回任何结果。我需要做什么来循环遍历数组数据?

1 个回复
最合适的回答,由SO网友:Johansson 整理而成

根据您所说的关于帖子完成的内容,您可以检查其状态是否为completed 如果是,请指向标题:

foreach ( $lessons as $lesson ){
    if( $lesson[\'status\'] == \'completed\' ){
        echo $lesson[\'post\']->post_title;
    }
}

结束

相关推荐

使用do_action_ref_array帮助回调

通过修改一些插件代码,我成功地获得了我想要的行为。然而,我想使用提供的钩子将我的修改移到插件代码之外,但我似乎无法使其工作。挂钩使用do\\u action\\u ref\\u数组。我能找到的访问php变量的唯一方法是向调试程序发送错误消息,这一事实无助于这种情况。日志文件。(像这样):function ra_func1($args) { $str = print_r($args, true); error_log($str); } add_action (