[PLUGIN] Page static with ramdom

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

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: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

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
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
SnorkY
Jr. Bludit
Posts: 5
Joined: Tue Oct 31, 2017 11:01 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: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

Is it this what you want: One page (for example the main page) which shows only one post (one video), which is selected randomly?
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
SnorkY
Jr. Bludit
Posts: 5
Joined: Tue Oct 31, 2017 11:01 am

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: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

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").
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
SnorkY
Jr. Bludit
Posts: 5
Joined: Tue Oct 31, 2017 11:01 am

Wow thank you very much.

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

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