0

The design for my site includes a large image slider on the homepage and a static image of equal dimensions and position on the inside pages.

To accomplish this I created a custom content type for the slider images and I modified the "Basic Page" content type to have a header image field. Then I created two views. One for the slider and one for the header image. The image slider view uses the flexslider module to create the slideshow. The header image view displays the the header image field with the nid as a contextual filter.

I placed the views in the header region using the blocks system and configured the image slider to only show on the front page, the static header image view on all but the front page.

This worked until I noticed that content that wasn't a basic page and therefore my header image view had nothing to display.

So my questions are.. Did I set up for my scenario the right way (using views and blocks etc) and how can I set a default image to display when content other than a page is being displayed? I did set a default image on the header image field but that only works for pages where I don't specify a value for the header image field. I'm using the Zen starter theme is this sort of thing usually done in the theme?

Thanks for any advice you can give me :)

  • I think your scenario is ok. Just define your default header image for other content types in your template.php file. – Jack-PL May 14 '13 at 16:56
  • @Jack-PL Can you tell me any more about how to define a default header image in my template.php file? And how to retrieve the non-default image when it's available? – Devin Crossman May 14 '13 at 17:04
1
/**
 * Override or insert variables into the page templates.
 *
 * @param $variables
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("page" in this case.)
 */
function YOURTHEME_preprocess_page(&$variables, $hook) {

    // it allows to use any template named with dashes i.e: page--type--blog.tpl.php
    $variables['theme_hook_suggestions'][] = 'page__type__'. $node->type;

    // define it as a false, so you won't need to use isset() in your template files
    $variables['default_header_image'] = false;

    // an array with content types with contextual header image
    $slider_pages = array(  'page__type__slider',
                            'page__type__sometype'
                            );

    if (!in_array($slider_pages, $variables['theme_hook_suggestions'])) {

        // default header image
        $variables['default_header_image'] = 'your image code';
    }
}

In your page.tpl file:

<!-- Page header -->
<div id="page_header" class="">

    <?php if ($default_header_image): ?>
        <div id="default_image"><?php print $default_header_image; ?></div>
    <?php endif; ?>

</div>
<!-- end page_header -->
0

Image fields can have default image, you should upload it in content type settings area, field settings. It will be treated as field's content when you will render empty field. In other words, should work out of the box, just the way you want it.

Views for slider is usual solution, there are modules to speed things up, but that's all.

For static images in basic pages, using views seems weird. Why not just put that field on top of your display and theme accordingly?

  • I have set the default image for the field but it will only show up if I'm visiting a "Basic Page" content type page. If I'm on the page of a different content type my view has nothing to show. In my design the header image is not right above the content area. The header image is at the top of the page and there are potential blocks between it and where the content will be displayed. Can I check for the existence of the header image field in my page.tpl.php and display it that way? – Devin Crossman May 14 '13 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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