random page list teaser

Permalink 3 users found helpful
Hi guys, just needing some advice if anyone can help...

I'm using the "page list teasers" template to create a sort of case study teaser in the footer of my site. It pulls in content from pages located under my "case studies" page, there are currently three child pages.

I only want one case study to show up at a time so i've set the page list to show 1 item, but i'm trying to get it to pull in the case studies from random, rather than selecting content from the first child page located under case studies.... hope that makes sense!

I'm guessing i need to randomize the array somewhere in the following:

if (count($cArray) > 0) { ?>   
   <?php   
   $excerptBlocks = ($controller->truncateSummaries ? 1 : null); //1 is the number of blocks to include in the excerpt
   $truncateChars = ($controller->truncateSummaries ? $controller->truncateChars : 0);
   $plth = Loader::helper('page_list_teasers', 'page_list_teasers');
   for ($i = 0; $i < count($cArray); $i++ ) {
      $cobj = $cArray[$i]; 
      if (version_compare(APP_VERSION, '5.4.1', '<')) {
         $target = '';
      } else {
         $target = 'target="'.$cobj->getAttribute('nav_target').'" ';
      }
      $title = $cobj->getCollectionName(); ?>
      <p><?php   $plth->output_page_blocks($cobj, $excerptBlocks, $truncateChars); ?></p>
<?php     }


After searching on here I tried adding:
shuffle($cArray);


Before:
for ($i = 0; $i < count($cArray);


But it's still just showing content from the first child page under case studies, rather than at random.

Any help would be appreciated, i'm still new to concrete so please go gentle :)

 
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Hi,
Welcome to C5! You are very close, but missing 2 things:

1) You need to *not* set the page list block to restrict the number of pages it shows to 1 (basically what you did is say "retrieve 1 page, then shuffle up that 1 page" -- which is like shuffling a single card instead of a whole deck -- you will always get that 1 card because it's the only one you're shuffling).

2) After you have pulled all of the pages and shuffled them (by putting "shuffle($cArray);" above the for loop like you have correctly done), you then want to restrict the loop to only go through one time, so change "for ($i = 0; $i < count($cArray); $i++ ) {" to this:
for ($i = 0; $i < 1; $i++ ) {


Also, note that when you customize a template like this in C5, a good practice is to first copy the file into YOURSITE/blocks/block_name/template/template_name.php -- this way your changes will not get overwritten if the package ever gets updated (it also keeps all of your customizations in the same place so it's easier to tell what's going on when you revisit the code 6 months from now).

Also also, I randomly stumbled upon this post (I'm the author of the Page List Teasers addon), but if you ever run into trouble with a specific addon in the future, you can always try the forum associated with that addon via the link in the sidebar of its marketplace page -- then the author gets an email notification about the question and will be more likely to respond.

Cheers,
Jordan
OliR replied on at Permalink Reply
Brilliant, that works perfect!

I'll be sure to follow your advice in future about both points you mentioned :)

Thanks for such a detailed reply!!
jordanlev replied on at Permalink Reply
jordanlev
Glad it worked!

If you don't mind and have a minute to spare, it would be great if you could post a quick review (or even just a rating) in the marketplace for this -- it would help other people find this addon, which I think is really useful. It unfortunately got a couple of bad ratings early on due to a bug which has since been fixed, so I'm trying to balance those out with some recent good ratings :)

Thanks,
Jordan
rc255 replied on at Permalink Reply
rc255
Hi there, I know this thread is from a while back, but I'm looking to do exactly the same thing as OliR. I have the code for teasers.php which is as follows:

<?php  defined('C5_EXECUTE') or die(_("Access Denied."));
$teaserBlockCount = ($controller->truncateSummaries ? 1 : null);
$teaserTruncateChars = ($controller->truncateSummaries ? $controller->truncateChars : 0);
$plth = Loader::helper('page_list_teasers', 'page_list_teasers');
$rssUrl = $plth->getRssUrl($b);
$th = Loader::helper('text');
//Note that $nh (navigation helper) is already loaded for us by the controller (for legacy reasons)
?>
<div class="ccm-page-list">
   <?php  foreach ($pages as $page):
      $title = $th->entities($page->getCollectionName());
      $url = $nh->getLinkToCollection($page);
      $target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
      $target = empty($target) ? '_self' : $target;
      $teaser = $plth->getPageTeaser($page, 'Main', $teaserBlockCount, $teaserTruncateChars);


But I cannot work out where this new bit of altered code should go:

if (count($cArray) > 0) { ?>   
   <?php   
   $excerptBlocks = ($controller->truncateSummaries ? 1 : null); //1 is the number of blocks to include in the excerpt
   $truncateChars = ($controller->truncateSummaries ? $controller->truncateChars : 0);
   $plth = Loader::helper('page_list_teasers', 'page_list_teasers');
   shuffle($cArray);
   for ($i = 0; $i < 1; $i++ ) {
      $cobj = $cArray[$i]; 
      if (version_compare(APP_VERSION, '5.4.1', '<')) {
         $target = '';
      } else {
         $target = 'target="'.$cobj->getAttribute('nav_target').'" ';
      }
      $title = $cobj->getCollectionName(); ?>
      <p><?php   $plth->output_page_blocks($cobj, $excerptBlocks, $truncateChars); ?></p>


I'm probably staring the answer in the face, but for the life of me I can't seem to nail it... If you can point me in theright direction, I'd be very grateful

Cheers

Rob
jordanlev replied on at Permalink Reply
jordanlev
The template code has probably changed a bit since this was first posted. I think to make this work you could change this line in teasers.php:
<?php foreach ($pages as $page): ?>

...to this:
<?php shuffle($pages); $page = $pages[0]; ?>


AND then remove this line:
<?php endforeach; ?>


You will probably want to remove the pagination and RSS stuff as well (or just make sure that you have pagination and RSS feed disabled in the Page List block settings when you add the block to the page), because those don't really make sense if you're only displaying a single randomly-selected page.
chris123uk replied on at Permalink Reply
chris123uk
I have just had to do this also so thought i would post the code I use:
I hard coded this into my page type: (not using the teaser)
<?php
  Loader::model('page_list');
  $pl = new PageList();
//$nh = Loader::helper('navigation');
  //$th = Loader::helper('text');
  //$ih = Loader::helper('image');
  //$pl->filterByAttribute('not_visable_in_search', '0'); // if this is not selected
  //$pl->filterByCollectionTypeHandle("trump_card");
  //$pl->sortByDisplayOrder();
  $pagelist = $pl->get();
  $total = $pl->getTotal();
  $randomPage = rand(1,$total);
  $i = 0;
  foreach ($pagelist as $page) {
    $i++;