1

Hey I am trying to use conditionals with Playa. I can't seem to get the following code to work

{if {exp:playa:total_parents channel='listing'} > '0'}
        <section class="listing-cards">
            <div class="gen-wrap">
                <header class="section-header light">
                    <h1>Our Listings</h1>
                    <h2>{title}</h2>
                </header>
              {exp:playa:parents channel="listing" status="not closed" show_expired="no"}
                    {lv_listing_card}
              {/exp:playa:parents}
        </div>
        </div><!--gen-wrap-->
        </section><!--listings-->
        {/if}

I don't want to show the .listing-cards section if there are no results. What am I doing wrong?

1

First to make it clear did u use it in {channel:entries} loop if not you should , and if you want to use it outside {channel:entries} loop you must add entry_id param

I am test your code on my localhost and it's work fine.

I hope this will help you.

Regard's.

1
  • Ya I do have it in the channel entries loop. I am getting errors like:A PHP Error was encountered Severity: User Warning Message: Invalid EE Conditional Variable: {if {exp:playa:total_parents entry_id="24" channel='listing' var_prefix="playabiLo4d5X"} Filename: libraries/Functions.php Line Number: 2762 Mar 13 '14 at 18:29
1

I'm not exactly sure on the full context but the problem could very well be the quotation marks around the 0. If you're comparing against a number then you don't need quotation marks as that will convert it to a string and will never match:

{if {exp:playa:total_parents channel='listing'} > 0}

If that doesn't work then you can probably simplify things a bit as the Playa loop—as with the Channel:Entries loop—will by default output nothing if there are no matches made. Using this as an example:

{!-- Parent (The channel with the Playa field) --}
{exp:channel:entries channel="news"}
    <h3>Article Media</h3>
    <ul>
        {exp:playa:children field="article_photos"}

            {if count == 0}
                {!-- Show substitute content --}
            {/if}

            {!-- Otherwise show the child entries --}
            {!-- Default behaviour: Nothing shown if nothing matched --}
            <li><a href="{path={channel_short_name}/{url_title}}">{title}</a></li>

        {/exp:playa:children}
    </ul>
{/exp:channel:entries}

So you should be able to use the regular count tag to check for the existence of any child entries (related articles).

If you want an alternative way to check for any child entries then you can use:

{if "{playa_parent:total_children}" > 0}
  ...
{/if}

Where playa_parent is the name of your Playa field.

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.