我使用的是Kirby CMS。
我正在尝试获取我的最新帖子,但前提是我的最晚帖子与我的$latest变量匹配。
$today = new DateTime("now"); //Declare today
$startdate = new DateTime("2013-06-30"); //Declare startdate
$interval = $startdate->diff($today); //Find the difference between startdate & today
$latest = $interval->format('%a'); //Declare $latest variable to be that interval, formatted as integer
$posts = $pages->find('posts')->children(); //Declare posts
$latestpost = $posts->find($latest); //Find post which matches the $latest variable
我正在尝试访问$latestpost->url();
,但前提是$latestpost
确实存在。当它现在没有的时候,它就变成了一个非对象。
因为我获取$latestpost
、isset
和defined
的方式不起作用。
否则我怎么能这样做?
根据$posts->find(..);
返回的内容(查看kirby的源,它将是null
或false
),可以使用许多不同的检查。
你最好的选择是只使用empty
,因为它会捕获大多数情况,它只会比特定的检查效率略低。
if (!empty($lastestpost)) {
$url = $latestpost->url();
}