Display regular pages on a static page that is not the homepage

Post Reply
koljaxyz
Ssr. Bludit
Posts: 16
Joined: Thu Apr 30, 2020 10:24 am

Hello! :)

I have the following page structure:

• Info (Static, defined as the frontpage in the backend)
• Projects (Static)
• Contact (Static)

• Project 1
• Project 2
• ...

In my index.php, I have:

Code: Select all

if ($WHERE_AM_I == 'page') {
    if ($page->slug() == 'information') {
        include(THEME_DIR_PHP.'info.php');
    }
    elseif ($page->slug() == 'projects') {
        include(THEME_DIR_PHP.'projects.php');
    }
    else {
        include(THEME_DIR_PHP.'page.php');
    }
} 
Now, in the projects template, I want to display the regular pages (the projects). However, if I use the foreach loop from the documentation, it doesn't work because it only displays the child pages of the displayed page:

Code: Select all

foreach ($content as $page) {
    echo $page->title();
    echo $page->content();
}
How can I display the regular pages on a static page?

Something like: Get all site children that are not static?
User avatar
SpegalDev
Ssr. Bludit
Posts: 22
Joined: Tue Jun 13, 2023 11:27 pm
Location: Michigan, USA
Has thanked: 1 time
Been thanked: 19 times
Contact:

A straightforward way to list specific pages on a template, especially when you need to distinguish between static and dynamic pages, requires understanding how Bludit handles page types and how to manipulate the API to filter pages based on your requirements.

Since you want to display regular pages on your static 'Projects' page and bypass the standard behavior that lists child pages, you'll need to modify how you fetch and filter the pages. Here’s a solution that uses Bludit's built-in functions to retrieve all pages and then filter out static pages:

Step 1: Fetch All Pages

First, get all pages, then you can filter them based on the type of the page. In Bludit, static pages are typically differentiated by their type, and you can check this using the `type()` method on a page object. Regular pages usually don't have a specific type set (or it could be 'published').

Step 2: Modify Your 'projects.php' Template

You need to adjust your `projects.php` to include a loop that filters and displays only the regular pages. Here’s how you could write it:

Code: Select all

<?php
    // Get all pages
    $allPages = $pages->getList();

    // Loop through all pages
    foreach ($allPages as $pageKey) {
        // Get the page object
        $page = new Page($pageKey);

        // Check if the page is not static
        if ($page->type() !== 'static') {
            echo '<h2>' . $page->title() . '</h2>';
            echo '<p>' . $page->content() . '</p>';
        }
    }
?>
This script does the following:
- `$pages->getList();` fetches keys of all pages.
- It then iterates through each key, instantiates a page object, and checks the type of each page.
- If the page is not static (`$page->type() !== 'static'`), it outputs the title and content.

This approach should effectively list all non-static pages in your 'Projects' page template. If you have any additional plugins or themes that modify standard page behaviors, you might need to adjust the code to accommodate those changes.
Image BluditLab — Explore a comprehensive collection of Bludit themes and plugins, offering both free and premium options to elevate your Bludit site. Find the perfect tools for customization, SEO, and more.
koljaxyz
Ssr. Bludit
Posts: 16
Joined: Thu Apr 30, 2020 10:24 am

🤔 This still does not show me the normal pages and I have no clue why. The pages I have created are listed within the "pages" tab. This should be correct.
If you have any additional plugins or themes that modify standard page behaviors, you might need to adjust the code to accommodate those changes.
I am building my own theme without any plugins and the only php code I have on the index.php is listed in my first post. The projects.php template itself is working and gets loaded. Do I miss something?

index.php

Code: Select all

<?php 
  if ($WHERE_AM_I == 'page') {
    if ($page->slug() == 'information') {
      include(THEME_DIR_PHP.'info.php');
    }
    elseif ($page->slug() == 'projects') {
      include(THEME_DIR_PHP.'projects.php');
    }
    else {
      include(THEME_DIR_PHP.'page.php');
    }
  } 
?>
projects.php

Code: Select all

<h1><?= $page->title() ?></h1>
<p>Projects Template</p>
<?php
    // Get all pages
    $allPages = $pages->getList();

    // Loop through all pages
    foreach ($allPages as $pageKey) {
        // Get the page object
        $page = new Page($pageKey);

        // Check if the page is not static
        if ($page->type() !== 'static') {
            echo '<h2>' . $page->title() . '</h2>';
            echo '<p>' . $page->content() . '</p>';
        }
    }
?>
It shows only the <h1> and the <p> tags.
User avatar
CCDzine
Ssr. Bludit
Posts: 13
Joined: Thu Sep 07, 2023 11:49 pm
Location: Sierra Nevada Mountains, Califonia
Has thanked: 4 times
Been thanked: 6 times
Contact:

Try using a different variable for a new instance of the Page class as $page is reserved as a global for the page you are in...

$post = new Page( $key ).
Greg Sweet
koljaxyz
Ssr. Bludit
Posts: 16
Joined: Thu Apr 30, 2020 10:24 am

CCDzine wrote: Sun May 05, 2024 12:43 am $post = new Page( $key ).
Does not work either :(
User avatar
CCDzine
Ssr. Bludit
Posts: 13
Joined: Thu Sep 07, 2023 11:49 pm
Location: Sierra Nevada Mountains, Califonia
Has thanked: 4 times
Been thanked: 6 times
Contact:

And you are then using $post, rather than $page, within the foreach loop?
koljaxyz
Ssr. Bludit
Posts: 16
Joined: Thu Apr 30, 2020 10:24 am

Code: Select all

<?php
    // Get all pages
    $allPages = $pages->getList();

    // Loop through all pages
    foreach ($allPages as $pageKey) {
        // Get the page object
        $post = new Page($pageKey);

        // Check if the page is not static
        if ($post->type() !== 'static') {
            echo '<h2>' . $post->title() . '</h2>';
            echo '<p>' . $post->content() . '</p>';
        }
    }
?>
User avatar
CCDzine
Ssr. Bludit
Posts: 13
Joined: Thu Sep 07, 2023 11:49 pm
Location: Sierra Nevada Mountains, Califonia
Has thanked: 4 times
Been thanked: 6 times
Contact:

The getList method is missing parameters.
Greg Sweet
Post Reply