Page 1 of 1

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

Posted: Mon Apr 22, 2024 9:54 pm
by koljaxyz
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?

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

Posted: Tue Apr 23, 2024 8:16 pm
by SpegalDev
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.

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

Posted: Sat Apr 27, 2024 12:51 pm
by koljaxyz
🤔 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.

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

Posted: Sun May 05, 2024 12:43 am
by CCDzine
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 ).

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

Posted: Sat May 11, 2024 10:46 am
by koljaxyz
CCDzine wrote: Sun May 05, 2024 12:43 am $post = new Page( $key ).
Does not work either :(

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

Posted: Mon May 13, 2024 3:34 am
by CCDzine
And you are then using $post, rather than $page, within the foreach loop?

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

Posted: Thu May 16, 2024 9:18 pm
by koljaxyz

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>';
        }
    }
?>