Search code examples
expressionengine

Check playa count in expression engine


I am using playa in expression engine. My structure is an h1 heading, then loop through the playa results. However, before I have the h1, I want to check if playa has any results. If yes, then display heading and results. If no, don't display heading.

How can I check the count of playa results before I use them?

<section id="related-products">
<h1 class="product-page-titles">Related Products</h1>
<div>
    {exp:playa:children field="related_products"}
    <article>
        <a href="/product/{url_title}" title="{title}">
            <h2>{title}</h2>
            <img src="{hero_image}"/>
        </a>
    </article>
    {/exp:playa:children}
</div>


Solution

  • Indeed!

    {if "{exp:playa:total_children field='related_products'}"}
        ...
    {/if}
    

    (As per the docs.)

    Also - please consider posting future questions on the dedicated ExpressionEngine StackExchange site. That is where the action is.