[PLUGIN] Page static with ramdom

Post Reply
SnorkY
Jr. Bludit
Posts: 5
Joined: Tue Oct 31, 2017 11:01 am

Tue Oct 31, 2017 2:36 pm

Hi!
I'm french user, sorry for my bad english.

I want create static page, with 1 random article inside.

URL : http://roulecommeunemerde.fr
User avatar
Edi
Site Admin
Posts: 1199
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

Tue Oct 31, 2017 6:24 pm

There is no plugin for this.

But it should be no problem to create a template using the PHP function mt_rand():

http://php.net/manual/fr/function.mt-rand.php
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
SnorkY
Jr. Bludit
Posts: 5
Joined: Tue Oct 31, 2017 11:01 am

Mon Nov 06, 2017 8:28 am

Thank you for your reply.

Unfortunately I do not master at Bludit and even less the dev :(
I'm looking for help for that ;-)
User avatar
Edi
Site Admin
Posts: 1199
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

Mon Nov 06, 2017 11:57 am

Is it this what you want: One page (for example the main page) which shows only one post (one video), which is selected randomly?
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
SnorkY
Jr. Bludit
Posts: 5
Joined: Tue Oct 31, 2017 11:01 am

Mon Nov 06, 2017 1:33 pm

Yes, so not on home.

I want create static page (/random) with random article (yes random one video for my exemple).
User avatar
Edi
Site Admin
Posts: 1199
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

Tue Nov 07, 2017 11:19 am

You can do it as follows:

1) Create an additional template for example random.php for your theme using the following:

Code: Select all

<?php

$pageNumber = 1; // First page number
$onlyPublished = true;

$listOfPages = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);

$number = count($listOfPages);

$random = mt_rand(0, $number-1);

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

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

?>
See for this also How to get URL of latest post.

This is only the code for the random display. The styling depends on your theme.

2) Modify the template index.php of your theme adding a condition to "if ($WHERE_AM_I=='page')" with the slug of the page with the random display (in our example "random"):

Code: Select all

<?php
	if ($WHERE_AM_I=='page') {
		if ( $Url->slug() == 'random' ) {
			include(THEME_DIR_PHP.'random.php');
		}
		else {
			include(THEME_DIR_PHP.'page.php');
		}
	}
	else {
		include(THEME_DIR_PHP.'home.php');
	}
?>
3) Create a static content (in our example with the title "Random").
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
SnorkY
Jr. Bludit
Posts: 5
Joined: Tue Oct 31, 2017 11:01 am

Tue Nov 07, 2017 2:05 pm

Wow thank you very much.

It's work ! :-)
Bier
Ssr. Bludit
Posts: 13
Joined: Sat Mar 17, 2018 10:21 am

Tue Mar 20, 2018 2:13 pm

Hi,

why this:

Code: Select all

$listOfPages = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);
$number = count($listOfPages);
$random = mt_rand(0, $number-1);
instead of:

Code: Select all

$listOfPages = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);
$random = mt_rand(0, $amountOfItems-1);
Post Reply