Counting images per node in theme
If you're using node template files to theme your output, you have a great deal of flexibility over what information you output and what it will look like. If you would like to have a content type appear differently than other content types, you can create a node template file specific to that content type.
Within the node template files, you can specify what the node teaser will look like independent of the full node view. This is generally done by evaluating either the $page or $teaser variable. The difference between the two are very subtle, but it can make a difference which one you use.
If you look around this site, you'll notice little differences in the way certain content types are displayed. For example, the picture and gallery content types have images that need to be formatted. Additionally, the teasers for those two display only the first uploaded image where the full node view displays the rest.
Node Type with Count
You might also notice that the node templates display the actual node type for both the teaser and the full node view. For Galleries, I thought it would be nice to display the actual number of images contained in the gallery (since only the first image displays in the teaser. This is actually very simple with one catch.
If you created the content type as described in the article on creating the Gallery content type, you would have selected 'Unlimited' for the number of images to allow to be uploaded. You could have, however, selected an exact number of images.
The difference between these two methods might not be apparent at first. On the surface, 'Unlimited' allows you to just keep adding images. A fixed number acts as a maximum value that anyone is allowed to add. The subtle difference appears to be that 'Unlimited' creates new Image Fields every time you click the 'Add Another' button. If you have selected, for example, 6 as the value of images to allow, then 6 empty fields are created for each node.
This difference means that simply counting the fields does not work for a specified number of images. If, however, you specify 'Unlimited', the themeing code is much more simple. The sample code below assumes that you'll be displaying the content type followed by the number of images in parentheses.
Code for Unlimited Images
This one's real easy.
<?php if ($node->type): ?>
<div class="node-type">
<h3><?php print ucfirst($node->type) . ": "; ?>
<?php
print ' (' . count($node->field_gallery_image) .' )';
</h3>
</div>
<?php endif; ?>A simple count function handles the dirty work for you. If the variable $node->type has a value, then print the value followed by the number returned by the count function performed on the CCK Imagefiled that you created for the Gallery content type.
Code for Fixed Number of Images
This is a little trickier. You need to loop through each imagefield in the node to check to see if it's empty and count the non-empty ones on the way. But it's still not rocket science. Here's the code for:
<?php if ($node->type): ?>
<div class="node-type">
<h3><?php print ucfirst($node->type) . ": "; ?>
<?php
$field_gallery_image_count = 0;
foreach ($node->field_gallery_image as $delta => $item) :
if (!$item['empty']) :
$field_gallery_image_count++;
endif;
endforeach;
print ' (' . $field_gallery_image_count .' Images)';
</h3>
</div>
<?php endif; ?>
This uses a variable ($field_gallery_image_count) which is initially set to '0'. Then you look through each of the fields (as $item) and increment the counter each time there's a non-empty imagefield. At the end of the loop, the value contained in the count variable is printed out in parentheses.
- Flappy's blog
- Login or register to post comments
-

Comments
#1 И все же, многое остается
И все же, многое остается туманным. Если не затруднит, распишите подробнее.
#2 Please post in
Please post in English.
(Пожалуйста, пост на английском языке. )
"Once in a while you get shown the light
in the strangest of places if you look at it right."
-Garcia/Hunter
#3 Заинтересован этой
Заинтересован этой тематикой.Много искал в сети, и наконец набрёл на свеженькое. Респект!!!
#4 Post in ENGLISH. It realy
Post in ENGLISH. It realy should not be necessary to say that more than once.
#5 Разумно и
Разумно и интересно.Уважуха!!!
#6 I'm surprised you can write,
I'm surprised you can write, since clearly you can not READ. Post in ENGLISH and only in ENGLISH.
#7 at least use
at least use translate.google.com and paste your translated text in here. meet us half way.
#8 Действительно любопытный
Действительно любопытный материал. Сейчас с добротными текстами в ру_нете проблемы.
#9 Я apprciate ваши комментарии.
Я apprciate ваши комментарии. Но это английский форум языке. Не могли бы вы, пожалуйста, используйте переводчик? Спасибо.
"Once in a while you get shown the light
in the strangest of places if you look at it right."
-Garcia/Hunter