Link to a Random Blog Post in a Sidebar Block
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.
- create a new view in administer » views » add tab.
- give it a name. I called mine "Random".
- click the "Block" section heading to get its settings.
- enable "Provide block"
- choose a "View type". I went with "List View" so that I could choose which fields display in the block.
- type in a title. This will be the title of the block. In this example I used "Random" as well.
- 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.
- click the "Fields" section heading to get its settings.
- add the fields you want. I chose just "Node: Title" since I wanted just links to the blog posts, not the text.
- click the "Filters" section heading to get its settings.
- select "Node: Type" and click "Add Filter"
- choose the content type(s) you want to appear in the block. In this example, I chose the 'blog' type.
- 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.
- click the "Sort criteria" section heading to get its settings.
- select "Random" and click "Add criteria".
- click "Save"
- 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
How about a Drupal 5 version please?
greetings, Martijn