0

I'm creating a custom view and I want to create thumbnails for the posts.

The problem is there is hundreds of posts and they all have 1-2 maybe 3 images in them. I want to pull the first image in the post and use it as a "post thumbnail" in a view.

Is this possible?

Thanks for any tips.

2 Answers 2

1

Yes select the cck field in the fields of the view and then in its options select number of images to display to one. This should do the job.

1
  • I'm not sure if this applies, this is plain HTML, none of the nodes are structured.
    – Talon
    Dec 1, 2011 at 4:32
0

Umm... are you saying that you have a content type that has html in it with several images along with text and you want to grab the img out of that content and display a thumbnail of it? Or, do you have a structured content type with image fields in it?

From your followup comment, it's html with images.

So..........

What you will have to do is a bit messy, but, if you look at the views create form, there is a Style Settings area with Theme Information. If you look in there, you can see what template files views uses to display your view.

You will have to find the template for the row of your view and edit that template.

In there, you will have to write some php code that looks through the field that contains your html, looking for the first img tag, grab it and output it, themed appropriately with css and all.

While complex, quite easily doable.

In the spirit of dull ax coding, this does the hacking:

$html='This is some random html with a [img src="/YOURIMAGEPATH" title="my image"] in it here and another one here [img src="/lrfmages/saloon320x50.jpg"]';

$content='<div>This is my html: ' . $html . '</div>';

$img=explode('<img ',$html);
$img=explode('>',$img[1]);

$content.='<div>Guts of first img tag: ' . $img[0] . '</div>';

$img=explode('src=',$img[0]);
$img=explode(' ', $img[1]);

$content.='<div>Guts of my src= attr: ' . $img[0] . '</div>';

$content.='<div>Therefore, my first image is <img src=' . $img[0] . '></div>';

return $content;

Perhaps not obviously, I used [img] above just so it wouldn't try to display an image in this form, change that to the appropriate less than IMG greater than in your code lol. And I wouldn't overload variables like I did above in production, I just wasn't feeling creative in names. And also, I am sure there is one regular expression out there that could do all of this, but this "shows the math" at least of showing how to split it up. Or so I hope. This worked here :)

Gack...looking at it, my quick code above assumes a space after the src= attribute, so it would fail if your image is set up as [img src="foo"] so you should craft this to your input specs. Fun eh? So you probably don't need to go that extra step if you don't mind having any other attributes that might be in with it, eg the title etc.

9
  • It's not a structured content type, it's just plain HTML with images inserted in.
    – Talon
    Dec 1, 2011 at 4:32
  • Where do I find the style settings information? I see it's style is set to "Unformatted"
    – Talon
    Dec 1, 2011 at 20:38
  • I can also add in a node:body type and that outputs the entire body text of the node. If I could control the output of the body like you mention I think that would work perfect. I'm just not sure where to control the output like that.
    – Talon
    Dec 1, 2011 at 20:39
  • In Drupal 6 anyway, a couple lines below the "Unformatted" line, there is a "Theme Information" link that when clicked shows you all the template files views uses to create its output. It's in there you have to muck around. You can be evil and edit the ones in views, or create a views directory in your theme and place appropriately named files there :)
    – Jimajamma
    Dec 1, 2011 at 20:42
  • Oh ok, I found that so do I just name the directory "views" so the full path should be /sites/all/themename/views/ What should the filename be?
    – Talon
    Dec 2, 2011 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.