I have a Matrix channel field to display sponsor images in a sidebar. I want to output 1 random image on page load and give user the option to only show specific sponsors on the homepage with Switch Field Type (these are essentially our client's featured sponsors that appear on homepage in addition to interior pages).
For some reason I can't get this code to work properly. Any help is much appreciated...
{exp:channel:entries}
{sponsors sort="random" limit="1"}
{if segment_1 == ""}
{if "{home_button}" == "y"}
<a href="{link}" target="_blank"><img src="{image}" alt="{alt}"/>
{/if}
{if:else}
<a href="{link}" target="_blank"><img src="{image}" alt="{alt}"/>
{/if}
{/sponsors}
{/exp:channel:entries}
UPDATED ANSWER: Found this to work...which gives me the ability to keep in "pages" channel without giving images their own channel.
{if segment_1 == ""}
{exp:channel:entries channel="pages"}
{!-- Load a random matrix row that has home button set to y --}
{sponsors sort="random" limit="1" search:home_button="y"}
<a href="{link}" target="_blank"><img src="{image}" alt="{alt}"/></a>
{/sponsors}
{/exp:channel:entries}
{if:else}
{exp:channel:entries channel="pages" dynamic="off"}
{!-- Load a random matrix row that has home button set to y --}
{sponsors sort="random" limit="1"}
<a href="{link}" target="_blank"><img src="{image}" alt="{alt}"/></a>
{/sponsors}
{/exp:channel:entries}
{/if}