Display images that have been created between any two story entries in the node-story.tpl.php

Last modified: February 21, 2008 - 12:25

I have a blog that basicly lets me publish stories and images for friends and family to see. The frontpage primarily shows the stories, while the images are handled through the Views module. Still I thought it would be nice if the tiny versions of the images added since the last story would show up on top of that story. Therefore I wrote this snippet. Its crude and uses two sql queries per story, but does its job. You might have to tweek it to fit your needs and I guess it could be improved a lot. You could probably limit it to one database query through subqueries), but I still think it is a neat feature.

In the admin/settings/image-page I added an image label called 'micro'. Change this to whatever you like.

Add this in the beginning of your node-story.tpl.php file

<div class="node_images">

<?php
$since
= $node->created;
$image_label = 'micro';
$header = "<div><h2>Images added since last story</h></div>";

$result = db_query("SELECT node.type, node.nid, node.created FROM node WHERE (node.created > $since) AND (node.type = 'story') ORDER by node.created LIMIT 0, 1");
while (
$node = db_fetch_object($result)) {
   
$until = $node->created;
}

/* Check if the current story  is the newest */
if ($until) {
   
$result2 = db_query("SELECT node.type, node.nid, node.created FROM node WHERE (node.created > $since) AND (node.created < $until) AND (node.type = 'image') ORDER by node.created  DESC");
} else {
   
$result2 = db_query("SELECT node.type, node.nid, node.created FROM node WHERE (node.created > $since) AND (node.type = 'image') ORDER by node.created  DESC");
};

while (
$nid = db_fetch_object($result2)) {
   
$output .= l(image_display(node_load(array('nid' => $nid->nid)), $image_label),'node/'.$nid->nid, array(), null, null, FALSE, TRUE);}
    print
$header;
    print
$output;
?>


</div>

 
 

Drupal is a registered trademark of Dries Buytaert.