Adding a conditional if statement to a variable field

I spent the entire night yesterday scouring the net and ended up banging my head against the wall. I’m probably missing something obvious, so I thought I’d write here in hopes of some guidance.

I’m a relative novice when it comes to PHP. I want to know how to add some conditionals to some fields that are repeating using the Advanced Custom Fields plugin for WordPress (with the Flexible Content and Repeater Fields add-ons). The repeater field has many sub-fields. I’m having trouble figuring out how to include or exclude some text alongside the sub-field whether or not the sub-field has any info plugged into it. For example, if the user doesn’t enter some info for the field ‘director’, how can make it not say “Directed by”?

<?php               
    if (get_field("production_history_flexible")){
        while (has_sub_field("production_history_flexible")){
            if (get_row_layout() == "season_date"){
                echo '<h2 class="surtitle">';
                the_sub_field("season_date_entry");
                echo '</h2>'; 
            }  

    if (get_row_layout() == "archive_entry"){  

    $rows = get_sub_field('archive_entry_repeater'); //Repeater Field Name

    if ($rows){ 
        foreach($rows as $row){
            echo '<p class="surtitle">'.$row['surtitle'].'</p>'; 
            echo '<h3>'.$row['title'].'</h3>'; 
            echo '<h4>by '.$row['writer'].'</h4>'; 
            echo '<ul>';
            echo '<li>Directed by '.$row['director'].'</li>'; 
            echo '<li>Performed by '.$row['performers'].'</li>';
            echo '<li>'.$row['dates'].'</li>'; 
            echo '<li>'.$row['venue'].'</li>'; 
            echo '</ul>'; 
            echo $row['description'];
            }   
        }
    }
    }
} ?>

Related posts

Leave a Reply

1 comment