[PLUGIN] Page static with ramdom
- Edi
- Site Admin
- Posts: 3121
- Joined: Sun Aug 09, 2015 5:01 pm
- Location: Zurich
- Has thanked: 54 times
- Been thanked: 78 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
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
- Edi
- Site Admin
- Posts: 3121
- Joined: Sun Aug 09, 2015 5:01 pm
- Location: Zurich
- Has thanked: 54 times
- Been thanked: 78 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
- Edi
- Site Admin
- Posts: 3121
- Joined: Sun Aug 09, 2015 5:01 pm
- Location: Zurich
- Has thanked: 54 times
- Been thanked: 78 times
- Contact:
You can do it as follows:
1) Create an additional template for example random.php for your theme using the following:
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"):
3) Create a static content (in our example with the title "Random").
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();
}
}
?>
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');
}
?>
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
Hi,
why this:
instead of:
why this:
Code: Select all
$listOfPages = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);
$number = count($listOfPages);
$random = mt_rand(0, $number-1);
Code: Select all
$listOfPages = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);
$random = mt_rand(0, $amountOfItems-1);