문제

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?

도움이 되었습니까?

해결책

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

다른 팁

I have never used Kirby, but I gave a quick look at its source code at https://github.com/bastianallgeier/kirbycms.

Most objects, including image objects, extend a generic obj class, which implements the Iterator interface.

This means that you can iterate on them with foreach, reset, current, next etc.

Besides that, the obj class contains a toArray method, which means it can be safely converted to an array, as you did in your code.

So, to take the first image, you just need to do:

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

Once you have the image object, you can get the image url with the url() method:

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

Give a look at the http://getkirby.com website. It seems well documented.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top