I’m trying to setup another part of my photography magazine, and I have some problems getting the content. The structure is:
and this is my code for the template book_reviews.php:
<?php $book_reviews = $pages->find('book_reviews')->grandChildren()->flip(); ?>
<ul>
<?php foreach($book_reviews as $review): ?>
<li>
<a href="<?php echo $review->url() ?>"><?php echo $review->author() ?></a> <?php echo html($review->title()) ?> <?php echo $review->month() ?>/<?php echo $review->year() ?>
</li>
<?php endforeach ?>
</ul>
I’ve read the kirby docs, but is not clear to me how to debug step by step what I’m doing. For example, if in my template I wrote this:
<?php $book_reviews = $pages ?>
<ul>
<?php foreach($book_reviews as $review): ?>
<li>
<a href="<?php echo $review->url() ?>"><?php echo $review->author() ?></a> <?php echo html($review->title()) ?> <?php echo $review->month() ?>/<?php echo $review->year() ?>
</li>
<?php endforeach ?>
</ul>
I get this:
If i wrote this
<?php $book_reviews = $pages->children()->find('book_reviews'); ?>
this:
<?php $book_reviews = $page->find('book_reviews'); ?>
or this:
<?php $book_reviews = $pages->find('book_reviews'); ?>
I get this:
I’d like to better understand how to get content and I’m not able to do that only with the online documentation. Is there a better way to learn and debug?
Thanks!
If you look at the documentation for the find
method, you will see that it can return either a Page or a Pages object, depending on what you feed it. In your case, you only try to get a single page, so the result is a single page. And you cannot loop through a single page but only through a collection.
What you really want to loop through (Yes, you want that! ), though, is the children of that page.
<?php $book_reviews = $pages->children()->find('book_reviews')->children(); ?>
or
<?php $book_reviews = page('books/book_reviews')->children(); ?>
1 Like
Thanks @texnixe,
it solved my issue.
I’m currently use this method to simplify the website tree and avoid duplicate content. As you can see in the screenshot on the first post I have a folder 0-home
that contains a home.txt with this text:
Title: #25 Altered Landscapes
----
Projects: archive/2016/altered-landscapes/projects
Then I have a template home.php
that took content from the archive. On top of the file I have:
<?php
$issue = page('archive/2016/altered-landscapes');
$editorial = page('archive/2016/altered-landscapes/editorial');
$projects = page('archive/2016/altered-landscapes/projects');
?>
that instruct the template where to find the content. Is it possible to took the path from the home.txt? So every time I need to publish a new issue I can keep the content in the archive and just change the home.txt and rsync just this file.
Thanks
Yes, of course, you can fetch the value from the home.txt file and pass that to the page helper instead:
$projects = page($page->projects());