0

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}
0

I currently see two problems with your code.

Assuming that this is the exact code that you are trying to run, the channel entries tag must have more parameters in order to display anything on the page correctly. Since you will be reusing the code on the homepage and other pages as well you will need to do something along these lines

{exp:channel:entries channel="sponsors" dynamic="no" limit="1"}

If you already had those things, there is a logic problem with your conditionals and the way you are loading in your matrix field. First your homepage conditional should be placed outside of the matrix calls. Let's say that you have:

name | homepage button
sponsor1 yes
sponsor2 no
sponsor3 no
sponsor4 yes
sponsor5 no

Your current code would try and randomly grab one of the matrix values. Then it goes to your conditional. If we are on the homepage, it will check if the randomly selected row is supposed to display or not, then the page is fully loaded. There is a 3 in 5 chance that nothing will display. You can fix this by changing where that conditional happens and adding the search parameter to a different matrix call.

{exp:channel:entries channel="sponsors" dynamic="no" limit="1"}

    {!-- If current page is the homepage --}
    {if segment_1 == ""}

        {!-- Load a random matrix row that has home button set to y --}
        {sponsors search:home_button="y" sort="random" limit="1"}
            <a href="{link}" target="_blank"><img src="{image}" alt="{alt}"/>
        {/sponsors}

    {!-- This is not the homepage --} 
    {if:else}

         {!-- Load any random sponsor including featured ones --}
         {sponsors sort="random" limit="1"}
            <a href="{link}" target="_blank"><img src="{image}" alt="{alt}"/>
         {/sponsors}
    {/if}

{/exp:channel:entries} 
7
  • Added code above, but looks like there might be a syntax error in "=y"? Shouldn't that be "y". Regardless, I tried both and nothing showing up on interior pages. The homepage is still displaying a random blank entry. Any idea what the issue is?
    – frshjb373
    Apr 27 '15 at 22:58
  • Just to cover all the bases, you do have an entry in whatever channel you have this data in correct? Is this channel dedicated only to these sponsors? What happens when you take the sort random out? Apr 28 '15 at 0:54
  • It's a channel field in my "pages" channel. Does it need to be in it's in own channel?
    – frshjb373
    Apr 28 '15 at 14:00
  • If you want to pull randomly from a large list of sponsors, yes. May 3 '15 at 15:29
  • That would generate a url for every sponsor, right? No need for url for each...they are just images.
    – frshjb373
    May 4 '15 at 17:18

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.