Search code examples
phparraysobjectslicekirby

Kirby CMS, how to get the first image from an Object with PHP?


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?


Solution

  • 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