Link to a Random Blog Post in a Sidebar Block

Last modified: September 2, 2006 - 07:22

Here is a simple view, which links to a random blog post. I rescued it from a comment describing a similar view for CCK content types, and simplified it a little. One link may not be enough for most, so feel free to use a number larger than one when creating your view.

  1. create a new view in administer » views » add tab.
  2. give it a name. I called mine "Random".
  3. click the "Block" section heading to get its settings.
  4. enable "Provide block"
  5. choose a "View type". I went with "List View" so that I could choose which fields display in the block.
  6. type in a title. This will be the title of the block. In this example I used "Random" as well.
  7. type in a number for the amount of nodes you want to display in the block. You can use 1 (number one) if you like.
  8. click the "Fields" section heading to get its settings.
  9. add the fields you want. I chose just "Node: Title" since I wanted just links to the blog posts, not the text.
  10. click the "Filters" section heading to get its settings.
  11. select "Node: Type" and click "Add Filter"
  12. choose the content type(s) you want to appear in the block. In this example, I chose the 'blog' type.
  13. select "Node: Published" and click "Add Filter". (I keep forgetting this one, but it's important, since you probably don't want to display unpublished nodes.
  14. click the "Sort criteria" section heading to get its settings.
  15. select "Random" and click "Add criteria".
  16. click "Save"
  17. click administer » blocks and enable the block you titled in step #6.

Use the following PHP code to import this view:

<?php
  $view
= new stdClass();
 
$view->name = 'Random';
 
$view->description = '';
 
$view->access = array (
);
 
$view->view_args_php = '';
 
$view->block = TRUE;
 
$view->block_title = 'Random';
 
$view->block_header = '';
 
$view->block_header_format = '1';
 
$view->block_footer = '';
 
$view->block_footer_format = '1';
 
$view->block_empty = '';
 
$view->block_empty_format = '1';
 
$view->block_type = 'list';
 
$view->nodes_per_block = '1';
 
$view->block_more = '0';
 
$view->block_use_page_header = FALSE;
 
$view->block_use_page_footer = FALSE;
 
$view->block_use_page_empty = FALSE;
 
$view->sort = array (
    array (
     
'tablename' => 'node',
     
'field' => 'random',
     
'sortorder' => 'ASC',
     
'options' => '',
    ),
  );
 
$view->argument = array (
  );
 
$view->field = array (
    array (
     
'tablename' => 'node',
     
'field' => 'title',
     
'label' => '',
     
'handler' => 'views_handler_field_nodelink',
    ),
  );
 
$view->filter = array (
    array (
     
'tablename' => 'node',
     
'field' => 'type',
     
'operator' => 'OR',
     
'options' => '',
     
'value' => array (
 
0 => 'blog',
),
    ),
    array (
     
'tablename' => 'node',
     
'field' => 'status',
     
'operator' => '=',
     
'options' => '',
     
'value' => '1',
    ),
  );
 
$view->exposed_filter = array (
  );
 
$view->requires = array(node);
 
$views[$view->name] = $view;
?>

How about a Drupal 5 version

Summit - May 27, 2008 - 15:35

How about a Drupal 5 version please?
greetings, Martijn

 
 

Drupal is a registered trademark of Dries Buytaert.