Page 1 of 1

How to get URL of latest post

Posted: Fri Nov 03, 2017 10:53 pm
by jonathanholvey
In Bludit 2 you can access the $pages array on the home page, which gives access to properties such as title, URL etc.

Is there a way to do the same from any page? Specifically, I'm trying to get the URL of the latest published post (page), so I can use it as a direct link in my theme's navigation menu.

Re: How to get URL of latest post

Posted: Fri Nov 03, 2017 11:14 pm
by diego
Hi,
this is for the pages with the type published, you need to use getList because this type is paginated.

Code: Select all

<?php

$pageNumber = 1; // First page number
$amountOfItems = 5; // Latest 5 items
$onlyPublished = true;
$listOfPages = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);

// Take only the keys
$listOfKeys = array_keys($listOfPages);

// Foreach key generate the page
foreach ($listOfKeys as $key) {
	$page = buildPage($key);
	echo $page->title();
	echo $page->permalink();
}

?>

Re: How to get URL of latest post

Posted: Fri Nov 03, 2017 11:17 pm
by diego
Now if you want to get the pages with the type static, is more easy because returns all the static pages, and on published pages could be a lot of them so you need to filter by pagenumber.

Code: Select all

<?php

$listOfKeys = $dbPages->getStaticDB($onlyKeys=true);

foreach ($listOfKeys as $key) {
	$page = buildPage($key);
	echo $page->title();
	echo $page->permalink();
}

?>

Re: How to get URL of latest post

Posted: Sat Nov 04, 2017 12:25 pm
by jonathanholvey
Thanks for that!