Hi !
I’m building a website where, on the homepage, news and projects are mixed but organised by date, then for each category there is an attributed page ‘projects’ and ‘news’ which I achieved.
However for the homepage, I don’t really know how could I say : for each ‘project’ and ‘news’…
For the project I’ve written the variable like this :
<?php
$projects = page('projects')->children()->visible()->sortBy('date', 'desc');
?>
then I called it like this :
<?php foreach($projects as $project): ?>
To sum up, I would need something like :
<?php
$projects = page('projects' AND 'news')->children()->visible()->sortBy('date', 'desc');
?>
Thank you !
You can merge both collections and then sort by date:
Two options:
//Merge two collections
<?php
$projects= page('projects')->children()->visible();
$news = page('news')->children()->visible();
$combined = new Pages(array($projects, $news))->sortBy('date', 'desc');
?>
//Appending stuff to a collection:
<?php
$combined = new Pages();
$combined->add(page('projects')->children()->visible());
$combined->add(page('news')->children()->visible());
$combined = $combined->sortBy('date', 'desc');
?>
Hi,
Thanks for your answer !
However I don’t really understand how to append the content of the var…
Just for you to understand how I did (and how I just know how to do… I’m new here haha) here’s my snippet for projects-call.
<?php
$projects = page('projects')->children()->visible()->sortBy('date', 'desc');
?>
<?php foreach($projects as $project): ?>
<article class='article-projects' ?> )">
<a href="<?php echo $project->url() ?>">
<h1><span><?php echo $project->title()->html() ?></span> </h1>
<p lang= <?php echo $site->language()->code() ?>
><?php echo $project->description2() ?> </p>
<?php if($image = $project->image('cover.jpg')): ?>
<img src="<?php echo $project->image('cover.jpg')->crop(900, 600,100)->url() ?>" alt="">
<?php else: ?>
<img src="<?php echo $project->image()->crop(900, 600,100)->url() ?>" alt="">
<?php endif ?>
</a>
</article>
<?php endforeach ?>
Do you know If I could use the same syntaxe ?
Thanks !
Well, after you have merged/appended the collection, you loop through them like in the above example.
<?php
$projects= page('projects')->children()->visible();
$news = page('news')->children()->visible();
$combined = new Pages(array($projects, $news))->sortBy('date', 'desc');
?>
<?php foreach($combined as $project): ?>
<article class='article-projects' ?> )">
<a href="<?php echo $project->url() ?>">
<h1><span><?php echo $project->title()->html() ?></span> </h1>
<p lang= <?php echo $site->language()->code() ?>
><?php echo $project->description2() ?> </p>
<?php if($image = $project->image('cover.jpg')): ?>
<img src="<?php echo $project->image('cover.jpg')->crop(900, 600,100)->url() ?>" alt="">
<?php else: ?>
<img src="<?php echo $project->image()->crop(900, 600,100)->url() ?>" alt="">
<?php endif ?>
</a>
</article>
<?php endforeach ?>
Thanks ! it works. However it doesn’t work with the
->sortBy('date', 'desc')
… do you know why ?
Oh, we have to change it a bit:
<?php
$projects= page('projects')->children()->visible();
$news = page('news')->children()->visible();
$combined = new Pages(array($projects, $news));
$combined = $combined->sortBy('date', 'desc');
?>
Or wrapped in parenthesis:
$combined = (new Pages(array($projects, $news)))->sortBy('date');
texnixe:
<?php
$projects= page(‘projects’)->children()->visible();
$news = page(‘news’)->children()->visible();
$combined = new Pages(array($projects, $news));
$combined = $combined->sortBy(‘date’, ‘desc’);
?>
This is so great ! Thanks a lot !!
Now just a last one
Is there a possibly to echo some stuff depending of the origin of the article ?
Because the content of news and article are totally different but the snippet name are sometimes the same.
So can I say something like :
<?php if($project = $combined is page('news')->children() ?>
//ECHO STUFF FOR NEWS
<?php else if($project = $combined is page('project')->children() ?>
//ECHO STUFF FOR PROJECT
?? THANKS !
<?php
if($project->parent()->uid() == 'news') {
// do stuff
} else {
// do sth. else
}
?>
1 Like
Thanks a lot !
I learnt a lot thanks to you.
Best greetings,
Valentin
1 Like