Simple on/off switches in Expression Engine
Posted by Rob on 25 Oct 2008 in ExpressionEngine | Permalink
So you’re using Expression Engine CMS for your site but you need the ability to turn some entry content on or off, depending on the entry. Here’s one way to do it.
OK, let’s say you want to put a link to your contact page in some entries, but not others. Normally you might just create a static link with the copy but that does take a bit of effort typing it out. Instead wouldn’t it be esaier just to choose whether or not to show that link by choosing from a drop down box?
Step 1 - create a custom field
- Add the Field name, we’ll call it ‘show_contact_link’
- Add the Field label, we’ll call it ‘Include contact’
- Change the field type to ‘Drop down list’ and choose ‘Populate the menu manually’
- In Select options add two values, ‘no’ and ‘yes’
- Set Default text formatting to ‘None’
- Save!
Now when you publish/edit you’ll have a nice shiny option…
Selecting yes or no is a lot easier that typing!
Step 2 - add the template code
{exp:weblog:entries weblog="{my_weblog}" limit="1"}<h1>{title}</h1>{extended}{if show_contact_link == ‘yes’}<p><a href="{path=site/contact}">Contact us today!</a></p>{/if}{/exp:weblog:entries}
So what’s this do? The conditional statement “if show_contact_link” checks to see if the answer is “yes” in your custom field - if true the contact link will be displayed. If false (ie “no") nothing will be displayed.
That was easy wasn’t it! You can use the concept to do lot’s of stuff, all you’re really doing is controlling what gets displayed from your weblog entry screen, the template does the rest of it.
Did you like this post? Share it with others...

Thanks for this really useful code!
I copied and pasted it and it when it didn’t work I was scratching my head, until I spotted that you have double quotation marks in front of the ‘yes’ in both cases—it should be a simple inch mark.