Page 1 of 1

[PLUGIN] Page static with ramdom

Posted: Tue Oct 31, 2017 2:36 pm
by SnorkY
Hi!
I'm french user, sorry for my bad english.

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

URL : http://roulecommeunemerde.fr

Re: [PLUGIN] Page static with ramdom

Posted: Tue Oct 31, 2017 6:24 pm
by Edi
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

Re: [PLUGIN] Page static with ramdom

Posted: Mon Nov 06, 2017 8:28 am
by SnorkY
Thank you for your reply.

Unfortunately I do not master at Bludit and even less the dev :(
I'm looking for help for that ;-)

Re: [PLUGIN] Page static with ramdom

Posted: Mon Nov 06, 2017 11:57 am
by Edi
Is it this what you want: One page (for example the main page) which shows only one post (one video), which is selected randomly?

Re: [PLUGIN] Page static with ramdom

Posted: Mon Nov 06, 2017 1:33 pm
by SnorkY
Yes, so not on home.

I want create static page (/random) with random article (yes random one video for my exemple).

Re: [PLUGIN] Page static with ramdom

Posted: Tue Nov 07, 2017 11:19 am
by Edi
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").

Re: [PLUGIN] Page static with ramdom

Posted: Tue Nov 07, 2017 2:05 pm
by SnorkY
Wow thank you very much.

It's work ! :-)

Re: [PLUGIN] Page static with ramdom

Posted: Tue Mar 20, 2018 2:13 pm
by Bier
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);