【问题标题】:Kirby CMS, how to get the first image from an Object with PHP?Kirby CMS,如何使用 PHP 从对象中获取第一张图像?
【发布时间】:2012-11-23 09:44:31
【问题描述】:

我目前正在用Kirby CMS 尝试一些东西。

现在我正在尝试用一些图片构建一个简单的博客,而我想要实现的是,博客文章中的第一张也是唯一的第一张图片显示在文章概述中,并附有简短的摘录来自文本。

所以我使用了 kirby 的 $page->images() 函数,它为我提供了帖子中所有图片的 url。但我只想要第一张照片!因为我找不到任何记录在案的选项来使用我尝试在 php 中执行的 kirby 函数。

我发现了什么:

-kirby 返回一个对象。
-我找不到任何 php-method 来从对象中切出某些东西,所以我尝试将其转换为数组:

$pictureArray = (array) $article->images();

-然后我尝试使用 array_slice,它返回了奇怪的结果,所以我仔细查看了我的数组。 print_r($pictureArray);返回:

Array ( [pagination] => [_] => Array ( [test.jpg] => image Object ( [meta] => Array ( ) [_] => Array ( [name] => test [filename] => test.jpg [extension] => jpg [root] => /home/exampledbq/www.example.com/kirby/content/01-articles/02-zweiter-Eintrag/test.jpg [uri] => content/01-articles/02-zweiter-Eintrag/test.jpg [parent] => files Object ( [pagination] => [_] => Array ( [article.txt] => variables Object ( [meta] => Array ( ) [_] => Array ( [name] => article [filename] => article.txt [extension] => txt [root] => /home/exampledbq/www.example.com/kirby/content/01-articles/02-zweiter-Eintrag/article.txt [uri] => content/01-articles/02-zweiter-Eintrag/article.txt [parent] => files Object *RECURSION* [modified] => 1354712997 [type] => content [variables] => Array ( [title] => Der zweite Eintrag [description] => Zusammenfassung. [published] => 18.12.2012 [tags] => Image, Article [text] => Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. (image: test.jpg) (image: test2.jpg) ) [filecontent] => Title: Der zweite Eintrag ---- Description: Zusammenfassung. ---- Published: 18.12.2012 ---- Tags: Image, Article ---- Text: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. (image: test.jpg) (image: test2.jpg) [languageCode] => en [template] => article ) ) [test.jpg] => image Object *RECURSION* [test2.jpg] => image Object ( [meta] => Array ( ) [_] => Array ( [name] => test2 [filename] => test2.jpg [extension] => jpg [root] => /home/exampledbq/www.example.com/kirby/content/01-articles/02-zweiter-Eintrag/test2.jpg [uri] => content/01-articles/02-zweiter-Eintrag/test2.jpg [parent] => files Object *RECURSION* [modified] => 1354713013 [type] => image [thumb] => image Object *RECURSION* [title] => test2 ) ) ) ) [modified] => 1354712200 [type] => image [thumb] => image Object *RECURSION* [title] => test ) ) [test2.jpg] => image Object ( [meta] => Array ( ) [_] => Array ( [name] => test2 [filename] => test2.jpg [extension] => jpg [root] => /home/exampledbq/www.example.com/kirby/content/01-articles/02-zweiter-Eintrag/test2.jpg [uri] => content/01-articles/02-zweiter-Eintrag/test2.jpg [parent] => files Object ( [pagination] => [_] => Array ( [article.txt] => variables Object ( [meta] => Array ( ) [_] => Array ( [name] => article [filename] => article.txt [extension] => txt [root] => /home/exampledbq/www.example.com/kirby/content/01-articles/02-zweiter-Eintrag/article.txt [uri] => content/01-articles/02-zweiter-Eintrag/article.txt [parent] => files Object *RECURSION* [modified] => 1354712997 [type] => content [variables] => Array ( [title] => Der zweite Eintrag [description] => Zusammenfassung. [published] => 18.12.2012 [tags] => Image, Article [text] => Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. (image: test.jpg) (image: test2.jpg) ) [filecontent] => Title: Der zweite Eintrag ---- Description: Zusammenfassung. ---- Published: 18.12.2012 ---- Tags: Image, Article ---- Text: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. (image: test.jpg) (image: test2.jpg) [languageCode] => en [template] => article ) ) [test.jpg] => image Object ( [meta] => Array ( ) [_] => Array ( [name] => test [filename] => test.jpg [extension] => jpg [root] => /home/exampledbq/www.example.com/kirby/content/01-articles/02-zweiter-Eintrag/test.jpg [uri] => content/01-articles/02-zweiter-Eintrag/test.jpg [parent] => files Object *RECURSION* [modified] => 1354712200 [type] => image [thumb] => image Object *RECURSION* [title] => test ) ) [test2.jpg] => image Object *RECURSION* ) ) [modified] => 1354713013 [type] => image [thumb] => image Object *RECURSION* [title] => test2 ) ) ) )

有人知道这是什么吗?我的意思是图片网址在那里,但是这些东西到底是从哪里来的?当我尝试回显 $page->images() 函数时,它只返回图片网址而不是整篇文章。

有人可以帮忙吗?也许甚至可以使用仅 kirby 的函数而不是 php 来实现这一点?

【问题讨论】:

    标签: php arrays object slice kirby


    【解决方案1】:

    查看kirby cheatsheet 会发现:您可以像在 jquery 中一样链接它...

    $page->images()->first()
    

    看看 http://getkirby.com/blog/cheat-sheet

    【讨论】:

      【解决方案2】:

      我从未使用过 Kirby,但我在 https://github.com/bastianallgeier/kirbycms 快速浏览了它的源代码。

      大多数对象,包括image 对象,都扩展了一个通用的obj 类,该类实现了Iterator interface

      这意味着您可以使用foreachresetcurrentnext 等对它们进行迭代。

      除此之外,obj 类包含一个 toArray 方法,这意味着它可以安全地转换为数组,就像您在代码中所做的那样。

      所以,要拍摄第一张图片,您只需要:

      $pictureArray = (array) $article->images();
      $firstImage = $pictureArray[0];
      

      一旦你有了image对象,你就可以使用url()方法获取图片的url:

      <img src="<?php echo $image->url(); ?>">
      

      看看http://getkirby.com 网站。它似乎有据可查。

      【讨论】:

        猜你喜欢
        • 2012-12-20
        • 2015-05-05
        • 1970-01-01
        • 1970-01-01
        • 2017-10-31
        • 2015-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多