从ACF循环访问POST对象时获取非对象的属性

时间:2019-03-13 作者:Gazz

我正在使用Root的Sage-Wordpress主题构建一个网站,该主题使用刀片模板引擎。

我在ACF中有一个转发器字段,其中有许多post对象。当我通过转发器foreach循环来检索post对象时,我得到一个Notice: Trying to get property of non-object 错误。当我关闭WP\\u Debug时,这些错误不存在,下面的代码按预期工作。

下面是使用的代码片段:


global $post; 

@foreach($page_column as $pages)


    {{-- If the link is internal --}}
    @if($pages[\'link_type\'] === "Internal")

        @foreach($pages as $post)

            @php 

            setup_postdata($post); 

            @endphp

            <li class="footer__list-item">

                <a class="footer__link" href="{{get_the_permalink($post->ID)}}">{{$post->post_title}}</a>

            </li>

            @php wp_reset_postdata(); @endphp

        @endforeach

    @endif

@endforeach
以下是输出内容:

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
Home

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
About

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
Locations

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
Case Studies

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
Downloads

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property \'post_title\' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
有趣的是,您可以看到代码按预期工作,因为在大多数错误消息下面,您可以看到输出的帖子标题。我也不明白为什么每页都会抛出多个错误,因为代码正在成功地从repeater字段检索所有页面。

根据查找错误时发现的情况,我在setup_postdata($post) 看看这是否能解决问题,但这似乎并不奏效。

我做错了什么?

1 个回复
SO网友:Gazz

没关系,几分钟后,我发现了问题所在。

而不是通过$post->ID 进入get_the_permalink, 我无法将任何参数作为setup_postdata($post) 处理好这件事。