1

I have a client who is an architect. I've created a content type called "project" that allows them to select multiple images to display on a page. These project items get promoted to the home page as teasers, however I'd like the customer to be able to select a featured image to display in the teaser, rather than having every image display.

Anyone know an easy way of doing this without having multiple image fields?

2 Answers 2

0

may be with some code in custom module, first image is featured

/**
 * Implementation of hook_node_view_alter().
 */
function HOOK_node_view_alter(&$build) {  
  if ($build['#view_mode'] == 'teaser' && isset($build['field_image'][1])) {
    foreach ($build['field_image'] AS $k => $item) {
      if (is_numeric($k) && $k > 0) {
        unset($build['field_image'][$k]);
      }
    }
  }
}

change "field_image" to your field_name

3
  • This doesn't create a featured image, this removes all other images, which potentially creates problems down the road. It's not sustainable solutions.
    – Letharion
    Sep 7, 2012 at 10:49
  • the process is in theme layer and not make anything persistent with real data, so I think this solution is OK. Please, explain situation with possible problems follow this solution, thank you. BTW I search for solution to make Image Widget were to set this per content type and display view, with no success in this time
    – svetlio
    Sep 7, 2012 at 11:18
  • The fundamental problem is that you are assuming that nothing else further down the line has no need for the data you are unseting, which is, in my personal opinion, a total hack.
    – Letharion
    Sep 7, 2012 at 11:34
0

I would say the easiest way is to make the first image also be the "featured" one, as then you already have most of the functionality built in.

It is then trivial to use a view that filters on "Image field: delta = 1", to give you only that one image, and then output it where appropriate.

A php savvy themer could also pull out the first image right in the template as well.

Your Answer

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

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