How to show all content from different subfolders on main page

Hello! I’m sorry for my english, it’s not my main language. I have a question if you have sometime to spare with me, and that is:

I want to show all my content on main page, so I have:

1_recipes

  • 1_content1
  • 1_content2

2_advices

  • 1_content1
  • 2_content2

What PHP code do I have to use in order to have this feature? Thanks in advance and have a nice day.

Hey, welcome to our forum!

That is definitely possible, but the code you need depends on how you want to show that content on the main page and on the data model (i.e. what fields are used in each of these pages) of each of these pages.

For example, if you just want to get a long list of all these pages with their titles, you would loop through the index:

<?php
foreach ( $site->index()->listed() as $p ) {
  echo $p->title();
}

Or maybe you want to create different sections per parent page, then you can fetch each of these pages individually:

<?php if ( $recipes = page('recipes') ) : ?>
  <section>
  <h1><?= $recipes->title() ?></h1>
    <?php if ( $recipes->hasListedChildren() ) : ?>
      <ul>
      <?php foreach ( $recipes->children()->listed() as $recipe ) : ?>
        <li>
          <?= $recipe->title()->html() ?>
          <!-- more fields here -->
        </li>
      <?php endforeach ?>
      </ul>
    <?php endif ?>
  </section>
<?php endif ?>

And in the same way for each other page and its children you want to show.


So we could better help you if you provide more detailed information about what exactly you want to achieve. What fields these pages have etc.
1 Like

Hello, thanks for helping me. I will try my best to explain what I’m trying to achieve.

So this is my layout for the main page (index):

First I’m trying to make my cards look like this:

Where you see text1, text2, text3 and so on till text9 is the place where I want to contain my cards.

It is possible to make the articles to follow like a train? I will add pagination to my index.

So let’s say that I have 100 articles, and you are on main page you see the latest article what I wrote, but if you are on page two you see the 10th article, 11th article etc.

Thanks in advance for your time and willingness to help :slight_smile: have a nice day.

Assuming you have read this cookbook page?

1 Like

Hi guys, sorry I had some health issues. I have looked on what you guys gave me, and I find myself nowhere. I can’t list all my articles from sub-folders on my homepage, it’s working only if I acces the sub-folders.

So…
When I am on home - I see no articles.

When i acces subfolder 1_morningrecipes
I see my articles from morning recipes

But i want on my default page to see all the articles from 1_morningrecipes, 2_desertrecipes, 3_dinnerrecipes etc

Please post the code you use on your home page, also, what is the file name of the content text file in the home folder and what is the template name where you are trying to fetch the subpages. Also please list the exact folder names of your content structure in the file system-

1 Like

So this is my content folder:

articole

This is my default.php:

<?php snippet('header') ?>
<?php snippet('body') ?>
<?php snippet('navpag') ?>
<?php snippet('footer') ?>
<?php snippet('aside') ?>

Article1, article2 till article 12 represent the grid layout from my css.

I have the following problems:

  1. The homefolder (named acasa) don’t show any of my articles written in folders 1-8.
  2. If I acces let’s say 1_miculdejun I can see my articles on the page.
  3. But it doesn’t work based on my grid layout, the content is duplicated, I think this need to be done with blueprints?
    So here is my body.php:
    <section class="content blog articol1">

     <?= $page->text()->kirbytext() ?>

     <?php foreach($page->children()->listed()->flip() as $article): ?>

     <article>
       <h1><?= $article->title()->html() ?></h1>
       <p><?= $article->text()->excerpt(300) ?></p>
       <a href="<?= $article->url() ?>">Read more…</a>
     </article>

     <?php endforeach ?>

   </section>


   <section class="content blog articol2">

   <?= $page->text()->kirbytext() ?>

   <?php foreach($page->children()->listed()->flip() as $article): ?>

   <article>
     <h1><?= $article->title()->html() ?></h1>
     <p><?= $article->text()->excerpt(300) ?></p>
     <a href="<?= $article->url() ?>">Read more…</a>
   </article>

   <?php endforeach ?>

   </section>

   <article class="articol3">
     <p>articol</p>
   </article>

   <article class="articol4">
     <p>articol</p>
   </article>

   <article class="articol5">
     <p>articol</p>
   </article>

   <article class="articol6">
     <p>articol</p>
   </article>

   <article class="articol7">
     <p>show</p>
   </article>

   <article class="articol8">
     <p>articol88</p>
   </article>

   <article class="articol9">
     <p>show</p>
   </article>

   <article class="articol10">
     <p>articol</p>
   </article>

   <article class="articol11">
     <p>articol</p>
   </article>

   <article class="articol12">
     <p>articol</p>
   </article>

Content file name is article.php

Sorry for being a pain, I am trying my best to understand things :D, pretty new to this domain. Thanks and have a nice day.

Again, what is the content file name inside the acasa folder (name of .txt inside)?

Which template is used for that folder?

If you use the same default template with the body.php snippet, then you will only ever fetch the children of the current page.

But you would have to get children of other pages, which I explained above: How to show all content from different subfolders on main page - #2 by texnixe

Only you would have to replace the folder names with your real folder names.