douyong7199 2012-12-05 14:25 采纳率: 0%
浏览 123
已采纳

Kirby CMS,如何使用PHP从Object获取第一个图像?

im currently trying out some stuff with Kirby CMS.

Right now I'm trying to build a simple blog with some pictures and what i want to achieve, is that the first, and only the first picture in the blog post, ist shown in the post overview together with a short excerpt from the text.

So Im using the $page->images() function from kirby, which gives me the urls from all the pictures in the post. But I want only the first picture! Because i cant find any documented options to do that with the kirby functions i tried to do it in php.

what i figured out:

-kirby returns an object.
-i couldnt find any php-method to slice something from an object so i tried to convert it to an array:

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

-then i tried to use array_slice, which returned strange results so i took a closer look at my array. print_r($pictureArray); returns this:

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 ) ) ) )

Anyone know what this is? i mean the image url is in there, but where the hell does all this stuff come from? when i try to echo the $page->images() function it returns only the image urls and not the whole article.

Can anybody help? maybe it is even possible to achieve this with kirby-only functions and not php?

  • 写回答

2条回答 默认 最新

  • dt246813579 2012-12-20 21:22
    关注

    A look at the kirby cheatsheet reveals: You can chain it as in jquery...

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

    Have a look at http://getkirby.com/blog/cheat-sheet

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等