I have installed the image module and configured it so users can upload photos (images) into a gallery.
I would like to display the latest N images in a block. I have noticed that there is a block 'Latest image' that displays the latest image but only 1.
I have looked at the module code and there are 2 methods that provide this functionality:
/**
* ##################################### Original Code #####################################
*/
/**
* Implementation of hook_block.
*
* Offers 2 blocks: latest image and random image
*/
function image_block($op = 'list', $delta = 0) {
switch ($op) {
case 'list':
$block[0]['info'] = t('Latest image');
$block[1]['info'] = t('Random image');
return $block;
case 'view':
if (user_access('access content')) {
switch ($delta) {
case 0:
$images = image_get_latest();
$block['subject'] = t('Latest image');
$block['content'] = l(image_display($images[0], IMAGE_THUMBNAIL), 'node/'. $images[0]->nid, array(), NULL, NULL, FALSE, TRUE);
break;
}
}
return $block;
}
}
/**
* Fetch the latest N image(s) - optionally from a given term.
*/
function image_get_latest($count = 6, $tid = 0) {
if ($tid != 0) {
$result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {term_node} tn LEFT JOIN {node} n ON n.nid=tn.nid WHERE n.type='image' AND n.status=1 AND tn.tid=%d ORDER BY n.changed DESC"), $tid, 0, $count);
}
else {
$result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.type='image' AND n.status=1 ORDER BY changed DESC"), 0, $count);
}
$output = array();
while ($nid = db_fetch_object($result)) {
$output[] = node_load(array('nid' => $nid->nid));
}
return $output;
}
/**
* ################################## End Original Code###################################
*/
By default the above code displays the latest (1) image only.
I have modified the code to display 2 images as follows:
/**
* ##################################### Modified Code #####################################
*/
/**
* Implementation of hook_block.
*
* Offers 2 blocks: latest image and random image
*/
function image_block($op = 'list', $delta = 0) {
switch ($op) {
case 'list':
$block[0]['info'] = t('Latest image');
$block[1]['info'] = t('Random image');
return $block;
case 'view':
if (user_access('access content')) {
switch ($delta) {
case 0:
$images = image_get_latest();
$block['subject'] = t('Latest image');
$block['content'] = l(image_display($images[0], IMAGE_THUMBNAIL), 'node/'. $images[0]->nid, array(), NULL, NULL, FALSE, TRUE);
/**
* ############## added this to append a second image to the content #################
*/
$block['content'] = $block['content'] . l(image_display($images[1], IMAGE_THUMBNAIL), 'node/'. $images[1]->nid, array(), NULL, NULL, FALSE, TRUE);
break;
}
}
return $block;
}
}
/**
* Fetch the latest N image(s) - optionally from a given term.
*/
function image_get_latest($count = 6, $tid = 0) {
if ($tid != 0) {
$result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {term_node} tn LEFT JOIN {node} n ON n.nid=tn.nid WHERE n.type='image' AND n.status=1 AND tn.tid=%d ORDER BY n.changed DESC"), $tid, 0, $count);
}
else {
$result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.type='image' AND n.status=1 ORDER BY changed DESC"), 0, $count);
}
$output = array();
while ($nid = db_fetch_object($result)) {
$output[] = node_load(array('nid' => $nid->nid));
}
return $output;
}
/**
* ################################## End Modified Code###################################
*/
Basically all that I added was this line to append to the output content the second image in the array: $block['content'] = $block['content'] . l(image_display($images[1], IMAGE_THUMBNAIL), 'node/'. $images[1]->nid, array(), NULL, NULL, FALSE, TRUE);
Now 2 images are displayed which is what I require but they are displayed vertically, one underneath the other with no padding or spacing, I would like them to be displayed horizontally with some padding, is this possible and how could I do this, I have checked the output and it writes out a div only.
Thanks in advance.
Comments
Comment #1
sunMarking as duplicate of #350511: Configurable number of images in Latest/Random blocks
Comment #2
mictang commented...............
Comment #3
mictang commentedfind a error:发现 一个 错误
$block['content'] = $block['content'] . l(image_display($images[1], IMAGE_THUMBNAIL), 'node/'. $images[1]->nid, array(), NULL, NULL, FALSE, TRUE);
output html:输出的
标签被转义了
<img src="http://localhost:8080/drupal/sites/default/files/images/1-5.thumbnail.jpg" alt="日本进口充气娃娃" title="日本进口充气娃娃" class="image image-thumbnail " width="100" height="100" />
correct :正确的应该是:
$block['content'] = $block['content'] . l(image_display($images[3], IMAGE_THUMBNAIL), 'node/'. $images[1]->nid, array('html' => TRUE), NULL, NULL, FALSE, TRUE);