How to include custom "blog archive"

Post Reply
calamaro
Jr. Bludit
Posts: 3
Joined: Wed Dec 18, 2019 6:42 pm

Wed Dec 18, 2019 7:24 pm

Hi,
I've created my first theme under Bludit 3.1.0 and I'm struggling a bit with including a custom php file. :roll:

The issue:

I have a one-page template which has an own section showing the latest three blog posts.

Section code:

Code: Select all

<!-- List Blog entries -->
	<?php foreach ($content as $page): ?>
		<a href="<?php echo $page->permalink(); ?>" class="col-lg-4 news-box">
			<?php if ($page->coverImage()): ?>
				<div class="image">
					<img src="<?php echo $page->coverImage(); ?>" alt="">
				</div>
			<?php endif ?>
			<?php if (!$page->isStatic() && !$url->notFound()): ?>
				<!-- Creator information -->
				<span class="news-info"><?php echo $page->user('nickname'); ?>, <?php echo $page->date(); ?></span>
			<?php endif ?>
				<!-- Title -->
				<h2 class="title"><?php echo $page->title(); ?></h2>
		</a>
	<?php endforeach ?>
When clicking on the title, you are led to the full article. This is working fine.

Below the three latest posts I would like to add a "Show more" button, which is linked to a separate "archive.php" file that is listing all blog posts.

But how can I assign an own permalink to the archive.php and how can it be included by the "page.php" when clicking on the "Read more" button as shown on the main page?
User avatar
Jay
Master Bludit
Posts: 106
Joined: Mon Feb 11, 2019 8:41 pm

Thu Dec 19, 2019 1:28 am

If I understood what you want to achieve, you'd need to fulfill couple things trying to list last couple posts on index page, and whole archive under let's suppose /archive URL
The snippet you provided lists all posts not only last three of them, and I assume you know that.

Basically in most templates index.php contains conditions including certain template files.
In first step I'd create a static page /archive that is going to list all blog posts on your site.
In index.php I'd make a condition checking whether you are on the home page or visiting the /archive page, and load proper php file
For example

Code: Select all

if ($WHERE_AM_I == 'page' && $page->slug() == 'archive' && $page->isStatic() ) {
			include(THEME_DIR_PHP.'archive.php');
elseif ($WHERE_AM_I == 'page') {
			include(THEME_DIR_PHP.'page.php');
}
else {
	include(THEME_DIR_PHP.'home.php');
}
This should cover listing blog posts and show their content.
You can place somewhere in your template manually a link to /archive

If you come up with other solution, plz share it with community.
calamaro
Jr. Bludit
Posts: 3
Joined: Wed Dec 18, 2019 6:42 pm

Fri Dec 20, 2019 9:40 pm

Hi, thank you very much for the hints. It helped to include the "archive.php" in a way I wanted.

I'm now listing all blog entries on the page "archive.php" using the following code from https://docs.bludit.com/en/dev-snippets/content-pages:

Code: Select all

<!-- List blog entries -->
      <?php
          // Page number, the first page is 1
          $pageNumber = 1;

          // Number of items to return
          $numberOfItems = 100;

          // Only get the pages with the status published
          $onlyPublished = true;

          // Get the list of keys of pages
          $items = $pages->getList($pageNumber, $numberOfItems, $onlyPublished);

          foreach ($items as $key): ?>

          <?php $page = buildPage($key); ?>

          <div class="col-lg-10 offset-lg-1 bottom_90 news-box list">
            <?php if ($page->coverImage()): ?>
              <div class="image">
                <img src="<?php echo $page->coverImage(); ?>" alt="">
              </div>
            <?php endif; ?>
              <span class="news-info"><?php echo $page->user('nickname'); ?>, <?php echo $page->date(); ?></span>
              <h2 class="title"><?php echo $page->title(); ?></h2>
              <p><?php echo $page->contentBreak(); ?> </p>
              <!-- Read more Button -->
              <?php if ($page->readMore()): ?>
                <a href="<?php echo $page->permalink(); ?>" class="site-btn float-left top_30"><?php echo $L->get('Read more'); ?></a>
              <?php endif; ?>
          </div>
        <?php endforeach; ?>
However, the pagination is not working now. I assume it's because the $pageNumber variable is set with "1"?

Any hints how can I list all blog entries with a working pagination?

When I use the following standard snippet it just says "Page not found".

Code: Select all

<?php
    foreach ($content as $page) {
        echo $page->title();
        echo $page->content();
    }
?>
User avatar
Edi
Site Admin
Posts: 1916
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

Sat Dec 21, 2019 1:12 pm

I thought the page or template archive.php should show all posts. Therefore I do not understand why it should work with the paginator.

home.php shows "all" posts with paginator. What should be the difference between home.php and archive.php?
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
Post Reply