I have created a function to go in template.tpl.php which generates a dropdown box, showing you the latest posts from a particular content type. The amount of items is variable, and you can restrain the number of characters of the titles so your layout will not change when a large title is entered. This is useful is you want to provide a clean and easy way for your visitors to access your nodes.
First I came up with this function to shorten the $title in a nice way:
// Function for cutting the parsed blogitems:
function wordCut($text, $limit, $end) {
if (strlen($text) > $limit) {
$txt1 = wordwrap($text, $limit, '[cut]');
$txt2 = explode('[cut]', $txt1);
$ourTxt = $txt2[0];
$finalTxt = $ourTxt.$end;
} else {
$finalTxt = $text;
}
return $finalTxt;
};
So now I can call wordCut() when I want to shorten a var displayed by my theme. Next thing to do is let the function render my selectbox, with the amount of options I want to display. When you select a title and release the mousebutton, Drupal will direct you to that particular $node_url, useful for displaying on the homepage.
// Function to create the selectbox with titles (uses wordCut(); )
function title_selectbox($numitems) {
$query = "SELECT n.created, n.title, n.nid, n.changed FROM node n WHERE n.status = 1 AND n.type = 'series' ORDER BY n.changed DESC LIMIT $numitems;";
$sql = db_rewrite_sql($query);
$result = db_query($sql);
$items = array();
echo '<form name="gototitle">';
echo '<select name="serietitles" onchange="window.location.href=this.options[this.selectedIndex].value;">';
echo '<option value="">Go to title ...</option>';
while ($anode = db_fetch_object($result)) {
$title = $anode->title;
echo '<option value="node/' . $anode->nid . '">'. wordCut($anode->title, 20, ' ...').'</option>';
}
echo '</select>';
echo '</form>';
}
There you go, you can now call this function on your homepage (for example) using <? title_selectbox(20) ?> to display this selectbox with the 20 most recent nodes of content type 'series'. You can change this to your likings off course.
Enjoy!
Comments
Thanks - lok forward to implementing it :)
How about `most recent items for that particular user' and the user being viewed (user profile)
http://drupal.org/node/165114