How to create a flexible hero block

So you want to offer your authors the ability to specify a “hero” style header for your entry, but have a fallback for when they don’t want one.

For the purposes of this example, we’ll assume your hero block will have an image, heading, and description.

Let’s set up some fields

Field Name Field Short Name Fieldtype
Hero Image hero_image File
Hero Heading hero_heading Text
Hero Description hero_description Text

Now add some code

{exp:channel:entries channel="my_channel" limit="1"}

{if hero_image}
  <div class="hero" style="background-image:url({hero_image});">



What we’re doing here is using a conditional {if hero_image} to check if the image exists. If it does, then we output the image as a background image using the style= parameter in the div container. If it doesn’t exist, then we just output our plain heading and description.

That’s it! All you need to do is add some style!

Rob Allen's avatar
Rob Allen

I've been creating web sites since 1998, and working with ExpressionEngine since 2007, building and maintaining sites for individuals, charities, organisations and businesses. I love using EE to do…

Comments 0

Be the first to comment!