Under most circumstances this would be an easy answer, but I have a slightly trickier segment I am working with.

The following code displays the category names if they are selected in the entry:

<div class="project-brief open-close">
    <h3>Results Type</h3>
        <div class="project-brief">
            <ul class="brief-list">
                <?php  $parents= array(); ?>
                {categories show_group="5"}
                <?php $parent_id = '{parent_id}'; if(!$parent_id) {$parents ['{category_id}'] = '{category_name}';} ?>

                <?php foreach ($parents as $key => $parent) { ?>
                <li><a href="/projects?category=<?php echo $key; ?>"><?php echo $parent; ?></a>
                    <ul class="brief-list-childs">
                        {categories show_group="5"}
                        {if parent_id == <?php echo $key; ?>}<li><a href="/projects?category={category_id}">{category_name}</a></li>{/if}
                <?php } ?>


However, the client wants to hide the entire section if none are selected in the entry.

I want to wrap the entire div in a conditional that checks to see if any of the categories are checked and display it only if it is not empty.

I would say it like this, but I can't seem to get the conditional right. Any guidance will be appreciated.

{If category count > 1}
   ... display the content

Not sure if this still works but might be worth a try:

{if "{categories}{category_name}{/categories}" != ""}

via: https://ellislab.com/forums/viewthread/136676/#673690

  • Hi Nathan, there are about ten categories in there but i'll give it a try.
    – forrest
    Jan 30 '15 at 21:04
  • Hi Nathan, this worked with one caveat. I needed to add this to the categories tag: show_group="5". Thanks for the timely help!
    – forrest
    Jan 30 '15 at 21:15
  • No worries, glad to help. Jan 31 '15 at 11:51

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.