4

Edited to be more specific in regards to my other modules/plugins:

I have a site that is basically set up like a printed calendar with an image on top and the content for the month below. The image and content come from two separate channels. The main page content is dynamic based on the url, and is working fine. I'm having trouble getting the image channel to display the way I want it to.

My url structure looks like this: events/calendar/date

I'm using Structure, Low Events to display the main page content, and Freebie to tell EE to ignore the date segment of the url, so that Low Events plays nice with Structure.

My Images channel is set up so that the url_title of my image entries is the same as the date segment of my page url for the month when i want the image to display. For example, my December 2012 calendar page url is events/calendar/2012-12 and my December image's url title is 2012-12. With this setup, i can use url_title="{freebie_3}" on my entries tag to display the proper image each month. This also works fine:

{exp:channel:entries channel="calendar_images" limit="1" url_title="{freebie_3}" dynamic="no"}

My problem is that there may be months when I don't have an entry in the images channel, but I still want to display a random image, rather than just leaving it blank.

As it is currently set up, if my freebie_3 segment doesn't match an existing url_title in my images channel, I get no result. Is there some way that I can get EE to display a random entry instead?

  • Did any of the answers help you? If yes, please mark the answer correct by clicking on the checkmark to the left of the answer. – Anna_MediaGirl Dec 22 '12 at 6:27
8

This is actually simpler than it seems. For this URL, events/calendar/2012-12 , use this code:

{exp:channel:entries channel="calendar_images" limit="1" orderby="random" dynamic="yes"}
Content
{/exp:channel:entries}

Assuming there's a template group called events, and a template called calendar, it'll display the entry matching the url_title 2012-12 automatically (ordered randomly, yes, but because it's already selecting just the right one, this doesn't matter).

If there ISN'T a match, it will display a random one from the channel.

If you want more control, you'll have to get more complicated, but this will work for what you describe.

ps Be careful with that url_title -- you can't have url_titles that are just numbers, so the "-" is very important in this case.

  • Unfortunately, that doesn't work because I'm using Structure and Freebie. I need the 2012-12 segment to display my calendar properly, so I'm using Freebie to tell EE to ignore that segment, so it doesn't break structure. Unfortunately, that means my channel entries tag doesn't know that the url title is there unless I explicitely set it using a Freebie tag. I probably should have been more specific in my initial question, but I didn't think the answer would be potentially so simple. – artmem Nov 30 '12 at 15:06
1

You should be able to use the "require_entry" parameter (see docs) to see if the url_title is valid. Combined with if no results you'd get something like:

{exp:channel:entries channel="calendar_images" limit="1" url_title="{segment_3}" dynamic="no" require_entry="yes"}
content
{if no_results} alternate content {/if}
{/exp:channel:entries}

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.