a random post on main page?

Post Reply
ntsu
Jr. Bludit
Posts: 3
Joined: Sat Aug 24, 2019 3:46 pm

Sat Aug 24, 2019 3:48 pm

hello,

i'm trying to show a random post on the home page each time i refresh. is that possible in a way? :| i would be thankful for any help. thanks alot!
User avatar
Edi
Site Admin
Posts: 1651
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

Sat Aug 24, 2019 3:52 pm

There is a thread about it:

Page static with random

Perhaps the code has to be adapted a little bit for Bludit 3.9.2.
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
ntsu
Jr. Bludit
Posts: 3
Joined: Sat Aug 24, 2019 3:46 pm

Sat Aug 24, 2019 3:59 pm

I saw it, but somehow it did not work!
User avatar
Edi
Site Admin
Posts: 1651
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

Mon Aug 26, 2019 11:51 am

There are some changes. You can use the following:

Code: Select all

<?php

global $pages;

$onlyPublished = true;
$pageNumber = 1;

$publishedPages = $pages->getList($pageNumber, $numberOfItems, $onlyPublished);

$random_key=array_rand($publishedPages);

$random_content = $publishedPages[$random_key];

foreach ($publishedPages as $pageKey){
	if ($pageKey == $random_content ) {
        $page = new Page($pageKey);
		echo $page->title();
		echo $page->permalink();
		echo $page->content();
	}
}

?>
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
ntsu
Jr. Bludit
Posts: 3
Joined: Sat Aug 24, 2019 3:46 pm

Fri Aug 30, 2019 11:25 pm

thats great! thank you very much!!
Post Reply