Support

Account

Home Forums Add-ons Repeater Field If No Repeater Rows

Solving

If No Repeater Rows

  • Hi there…

    I’m trying to create a php loop that cheecks if rows exist and if they do not display text such as ‘Sorry no rows’. My repeater is nested, meaning a repeater inside a repeater.

    Thanks in advance!

    Here’s the code I have thats not working…

    <?php if( have_rows('add_career_opportunities', 'option') ): ?>
    
    <div id="career-single">
    
        <?php while( have_rows('add_career_opportunities', 'option') ): the_row();
    
        //Variables
        $title = get_sub_field('job_title');
        $description = get_sub_field('job_description', 'option'); ?>
    
            <h2 id="job-title">
                <?php echo $title; ?>
            </h2>
    
            <p id="job-description">
                <?php echo $description; ?>
            </p>
    
            <h3 id="job-requirements-heading">
                Requirements
            </h3>
    
            <?php if( have_rows('job_requirements', 'option')): ?>
    
                <ul id="job-requirements">
    
                    <?php while( have_rows('job_requirements', 'option') ): the_row();
    
                    $requirement = get_sub_field('job_requirement', 'option'); ?>
    
                    <li>
                        <?php echo $requirement; ?>
                    </li>
    
                <?php endwhile; ?>
                </ul>
    
            <?php else:
                echo '<h1>Sorry, we are not currently hiring. Please check back often for new opportunities with DesRosiers Architects!</h1>';
            endif; ?>
            </div>
        <?php endwhile; ?>
    <?php endif; ?>
  • Hi @robertrhu

    I have just tried a simplified version of a similar repeater loop and the if else conditional seems to work as expected.

    Would you mind sharing a JSON export of your field settings so that I can test this out using the same configuration?

  • Hi James,

    Thank you for the response.

    Here you go…

    Thanks!
    Robert

    [
        {
            "key": "group_58ad31aed23da",
            "title": "Employee Profiles",
            "fields": [
                {
                    "key": "field_58ad329495dc4",
                    "label": "Add A Profile",
                    "name": "add_profile",
                    "type": "repeater",
                    "instructions": "Add a new employee profile here",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "collapsed": "field_58ad333095dc6",
                    "min": 0,
                    "max": 0,
                    "layout": "block",
                    "button_label": "Create New Employee Profile",
                    "sub_fields": [
                        {
                            "key": "field_58ad330995dc5",
                            "label": "Employee Photo",
                            "name": "employee_photo",
                            "type": "image",
                            "instructions": "Upload a photo for this employee profile here",
                            "required": 1,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "return_format": "url",
                            "preview_size": "thumbnail",
                            "library": "all",
                            "min_width": "",
                            "min_height": "",
                            "min_size": "",
                            "max_width": "",
                            "max_height": "",
                            "max_size": "",
                            "mime_types": ""
                        },
                        {
                            "key": "field_58c5abab581e3",
                            "label": "Employee Photo Alt Text",
                            "name": "employee_photo_alt",
                            "type": "text",
                            "instructions": "Add image alt text for  this profile image",
                            "required": 1,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "",
                            "prepend": "",
                            "append": "",
                            "maxlength": ""
                        },
                        {
                            "key": "field_58ad333095dc6",
                            "label": "Employee Name",
                            "name": "employee_name",
                            "type": "text",
                            "instructions": "Enter the employee's name here",
                            "required": 1,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "i.e. John Doe",
                            "prepend": "",
                            "append": "",
                            "maxlength": ""
                        },
                        {
                            "key": "field_58ad33cc95dc7",
                            "label": "Job Title",
                            "name": "job_title",
                            "type": "text",
                            "instructions": "Enter the job title for this employee here",
                            "required": 1,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "i.e Architect",
                            "prepend": "",
                            "append": "",
                            "maxlength": ""
                        },
                        {
                            "key": "field_58ad340895dc8",
                            "label": "Bio",
                            "name": "employee_bio",
                            "type": "textarea",
                            "instructions": "Enter a short bio for this employee here",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "",
                            "maxlength": "",
                            "rows": "",
                            "new_lines": ""
                        },
                        {
                            "key": "field_58c5a9d2b1522",
                            "label": "Profile Slug",
                            "name": "profile_slug",
                            "type": "text",
                            "instructions": "Enter a link slug for this profile here. This must be all lower-case with no spaces.",
                            "required": 1,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "i.e. louisdesrosiers",
                            "prepend": "",
                            "append": "",
                            "maxlength": ""
                        }
                    ]
                }
            ],
            "location": [
                [
                    {
                        "param": "options_page",
                        "operator": "==",
                        "value": "employee-profiles"
                    }
                ]
            ],
            "menu_order": 0,
            "position": "normal",
            "style": "default",
            "label_placement": "top",
            "instruction_placement": "label",
            "hide_on_screen": "",
            "active": 1,
            "description": ""
        }
    ]
  • And a little additional info…

    1. Its not kicking an error
    2.Rather its simply not printing the <?php else code:
    <h1>Sorry, we are not currently hiring. Please check back often for new opportunities with DesRosiers Architects!</h1>

  • Hi @robertrhu ,

    That is quite odd.

    Kindly share submit a ticket at support@advancedcustomfields.com for some personalised support.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.