From the archives ------ snip ------ */ require_once 'classes/LightPressPlugin.php'; class RandomPosts extends LightPressPlugin { var $constructor_args = array( 'date_format' =>'custom date format', 'limit' =>'number of posts to show', 'category' =>'restrict to one or more categories, leave empty to use every category, format: cat_id:cat_id:cat_id', 'excerpt_length' => "maximum length in words for the excerpt, set to '0' to disable", 'ignore_excerpt' => 'ignore the stored excerpt, and always use the post content to generate a new one'); var $default_context = LP_CONTEXT_ALL; var $description = 'Display n random posts'; var $active = true; var $hooks = array('sidebar'); var $date_format; var $limit = 5; var $category = ''; var $excerpt_length = 25; var $ignore_excerpt = false; function RandomPosts(&$frontend, $args, $dummy_run=false) { $this->LightPressPlugin($frontend, $args, $dummy_run); if (empty($this->excerpt_length)) $this->excerpt_length = 0; else $this->excerpt_length = (int)$this->excerpt_length; $this->category = trim($this->category); if (!empty($this->category)) { $category = array(); foreach (explode(':', $this->category) as $cat) $category = trim($cat); $this->category =& $category; } } function run($hook, &$hook_data) { $frontend =& $this->_frontend; $tpl =& $frontend->tpl; $db =& $frontend->db; $date_format = (empty($this->date_format) ? $frontend->options['date_format'] : $this->_date_format); $tpl->setFile('plugin_randomposts', 'plugins/random_posts.xml'); $q = "select p.id, p.post_title as title, p.post_name as name, unix_timestamp(p.post_date) as tstamp, p.post_category, p.post_content as content, p.post_excerpt as excerpt from {$frontend->tables['posts']} p "; $w = ''; if (is_array($this->category)) { $q .= "inner join {$frontend->tables['post2cat']} p2c on p.id=p2c.post_id\n"; $w = " and p2c.category_id in (" . implode(', ', $this->category) . ")"; } $q .= " where {$frontend->get_posts_clause} and {$frontend->only_published} $w order by rand() limit {$this->limit}"; if (!$db->query($q)) return $this->hide(); $posts = array(); foreach ($db->all() as $k => $v) { $v['permalink'] = $this->_frontend->getPermalink($v['id'], $v['name'], $v['tstamp'], $v['post_category']); $v['date'] = strftime($date_format, $v['tstamp']); $v['short_date'] = strftime($frontend->options['short_date_format'], $v['tstamp']); if ($this->ignore_excerpt || empty($v['excerpt'])) $v['excerpt'] = stripslashes($v['content']); else $v['excerpt'] = stripslashes($v['excerpt']); $v['excerpt'] =& $this->_frontend->getExcerpt($v['excerpt'], $this->excerpt_length); unset($v['content']); $posts[] = $v; } $tpl->parseBlock('PLUGIN_RANDOMPOSTS_POST', 'plugin_randomposts_post', $posts, 'plugin_randomposts'); $tpl->parse('PLUGIN_RANDOMPOSTS', 'plugin_randomposts'); } } ?>