2

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'];
            }   
        }
    }
    }
} ?>

1 Answer 1

1

Use :

if(trim($row['director']!=''))
{
    echo '<li>Directed by '.$row['director'].'</li>'; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

This code is wrong. The closing ) is misplaced. Personally for better readability i'd use: if ( ! empty ( trim ( $row['director'] ) ) { ..... }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.