0
votes

I'm a PHP novice and I can't quite figure out where to put in some code within my PHP to wrap the outputted content in a div. As it stands, this outputs the echo twice, each around the "season_date" row and the "archive_entry" row. I'd like to wrap the entire thing in one div.

It's probably something really obvious, but I can't seem to figure out what I'm doing wrong...

Here's the code:

<?php               
    if (get_field("production_history_flexible")){
        while (has_sub_field("production_history_flexible")){
            echo '<div class="archive-wrapper">'; // BEGIN DIV HERE??
            if (get_row_layout() == "season_date"){
                echo '<h2 class="season-date">';
                the_sub_field("season_date_entry");
                echo '</h2>'; 
            }  

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

    $rows = get_sub_field('archive_entry_repeater');

    if ($rows){ 
        foreach($rows as $row){ }
    }

    // Surtitle
    if(trim($row['surtitle']!=''))
        { echo '<p class="surtitle">'.$row['surtitle'].'</p>'; }    

    // Title
    if(trim($row['title']!=''))
        { echo '<h3 class="season-history-show-title">'.$row['title'].'</h3>'; }    

    // Writer
    if(trim($row['writer']!=''))
        { echo '<h4 class="season-history-writer">by '.$row['writer'].'</h4>'; }

    echo '<ul>';

    // Director
    if(trim($row['director']!=''))
        { echo '<li>Directed by '.$row['director'].'</li>'; }

    // Performers
    if(trim($row['performers']!=''))
        { echo '<li>Performed by '.$row['performers'].'</li>'; }        

    // Co-Pro
    if(trim($row['co-pro']!=''))
        { echo '<li>Co-produced with '.$row['co-pro'].'</li>'; }        


    // Dates
    if(trim($row['dates']!=''))
        { echo '<li>'.$row['dates'].'</li>'; }

    // Venue
    if(trim($row['venue']!=''))
        { echo '<li>'.$row['venue'].'</li>'; }

    echo '</ul>'; 

    }
    echo '</div>'; // END DIV WRAPPER HERE?

    }
} ?>

(I'm using the Advanced Custom Fields plugin for Wordpress with the flexible content and repeater content add-ons if that matters.)

2

2 Answers

2
votes

Try this code, which will open a div each time a new season is found:

<?php
    $first = true;
    if (get_field("production_history_flexible")){
        while (has_sub_field("production_history_flexible")){
            if (get_row_layout() == "season_date"){
                if(!$first)
                    echo '</div>';
                else
                    $first = false;
                echo '<div class="archive-wrapper">';
                echo '<h2 class="season-date">';
                the_sub_field("season_date_entry");
                echo '</h2>'; 
            }  

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

    $rows = get_sub_field('archive_entry_repeater');

    if ($rows){ 
        foreach($rows as $row){ }
    }

    // Surtitle
    if(trim($row['surtitle']!=''))
        { echo '<p class="surtitle">'.$row['surtitle'].'</p>'; }    

    // Title
    if(trim($row['title']!=''))
        { echo '<h3 class="season-history-show-title">'.$row['title'].'</h3>'; }    

    // Writer
    if(trim($row['writer']!=''))
        { echo '<h4 class="season-history-writer">by '.$row['writer'].'</h4>'; }

    echo '<ul>';

    // Director
    if(trim($row['director']!=''))
        { echo '<li>Directed by '.$row['director'].'</li>'; }

    // Performers
    if(trim($row['performers']!=''))
        { echo '<li>Performed by '.$row['performers'].'</li>'; }        

    // Co-Pro
    if(trim($row['co-pro']!=''))
        { echo '<li>Co-produced with '.$row['co-pro'].'</li>'; }        


    // Dates
    if(trim($row['dates']!=''))
        { echo '<li>'.$row['dates'].'</li>'; }

    // Venue
    if(trim($row['venue']!=''))
        { echo '<li>'.$row['venue'].'</li>'; }

    echo '</ul>'; 

    }

    }
    if(!$first)
        echo '</div>';
} ?>
0
votes

I had a similar issue, with Flexible Content fields that I needed to display in a 'modular' style page - eg the page content has a wrapper around content sections, but the Call To Action and Testimonial Slider need to close that wrapper before it starts and open it afterwards.. so only the Call To Action and Testimonials are full width.

Here is what I came up with, although, there is likely a more elegant way to code this.. this works :)

                    <?php
                    $module_open; // set var
                    $module_close; // set var
                    
                    function content_module_open(){
                        global $module_open;
                        global $module_close;
                        
                        if($module_open !== TRUE){
                            echo '<div class="content-module">';
                            $module_open = true; 
                            $module_close = false; 
                        }
                    }
                    
                    function content_module_close(){
                        global $module_open;
                        global $module_close;
                        
                        if($module_close !== TRUE){
                            echo '</div><!-- content-module -->';
                            $module_close = true; 
                            $module_open = false;
                        }
                    }
                    
                ?>
                <?php if (have_rows('flexible_content_types')): ?>
                                                
                    <?php while (have_rows('flexible_content_types')): the_row(); ?>

                        <?php if (get_row_layout() == 'heading_h1'): ?>
                            <?php content_module_open();?>
                
                            <h1 class=""><?php the_sub_field('heading'); ?></h1>
            
                        <?php elseif (get_row_layout() == 'heading_h2'): ?>
                            <?php content_module_open();?>
                    
                            <h2 class=""><?php the_sub_field('heading'); ?></h2>
            
                        <?php elseif (get_row_layout() == 'paragraph_content'): ?>
                            <?php content_module_open();?>
                    
                            <?php the_sub_field('content'); ?>

                        <?php elseif (get_row_layout() == 'faq_accordion'): ?>
                            <?php content_module_open();?>                      
        
                        <?php if (have_rows('faqs')): ?>
                        <?php $count = 0 ?>
                        <div class="accordion" id="faqAccordion">
                            <?php while (have_rows('faqs')): the_row(); ?>
                            <?php $count++; ?>
                            <div class="card">
                                <div class="card-header" id="heading<?php echo $count; ?>">
                                    <h2 class="clearfix mb-0">
                                        <a class="btn btn-link" role="button" data-toggle="collapse" data-target="#collapse<?php echo $count; ?>" aria-expanded="<?php if ($count === 1){echo 'true';}else{echo 'false';} ?>" aria-controls="collapse<?php echo $count; ?>">
                                            <?php the_sub_field('question');echo $count; ?> 
                                            <i class="fas fa-<?php if ($count === 1){echo 'plus';}else{echo 'minus';} ?>"></i>
                                        </a>                                    
                                    </h2>
                                </div>
                                <div id="collapse<?php echo $count; ?>" class="collapse <?php if ($count === 1){echo 'show';} ?>" aria-labelledby="heading<?php echo $count; ?>" data-parent="#faqAccordion">
                                    <div class="card-body"><?php the_sub_field('answer'); ?></div>
                                </div>
                            </div>
                            <?php endwhile; ?>
                        </div>
                        <?php endif; ?>
                   
                      <?php elseif (get_row_layout() == 'timeline'): ?>
                        <?php content_module_open();?>                          
                        
                        <?php if (have_rows('timeline_items')): ?>
                                <ul class="timeline">
                                <?php $time = 0 ?>
                           <?php while (have_rows('timeline_items')): the_row(); ?>
                                <?php $time++; ?>
                                <li class="<?php if ($time % 2 == 0){echo "timeline-inverted";} ?>">
                                    <div class="timeline-date"><?php the_sub_field('year'); ?></div>
                                  <div class="timeline-badge"><i class="fa fa-map-signs"></i></div>
                                  <div class="timeline-panel">
                                     <div class="timeline-heading">
                                        <h4 class="timeline-title"><?php the_sub_field('title'); ?></h4>
                                     </div>
                                     <div class="timeline-body">
                                        <p><?php the_sub_field('content'); ?></p>
                                     </div>
                                  </div>
                               </li>

                            <?php endwhile; ?>
                                </ul>
                        <?php endif; ?>
                    <?php elseif (get_row_layout() == 'association_list'): ?>
                        <?php content_module_open();?>
                    
                            the_sub_field('associations');
                                the_sub_field('association_logo');
                                the_sub_field('assoc_title');
                                the_sub_field('assoc_description');
                    <?php elseif (get_row_layout() == 'call_to_action'): ?>
        
                        <?php content_module_close(); ?>
        
                        <?php if($module===0){echo $close;} ?>
                        
                        <div class="content-cta cta-tabs cta-<?php the_sub_field('cta_style') ?> text-center">

                            <?php if (get_sub_field('cta_heading')): ?>
                                <h1 class=""><?php the_sub_field('cta_heading') ?></h1>
                            <?php endif; ?>                         

                            <?php if (get_sub_field('sub_text')): ?>
                                <p class="lead"><?php the_sub_field('sub_text') ?></p>
                            <?php endif; ?>

                            <?php if (get_sub_field('button_text') && get_sub_field('button_link')): ?>
                                <a href="<?php the_sub_field('button_link') ?>" class="btn btn-lg <?php the_sub_field('button_color') ?>"><?php the_sub_field('button_text') ?></a>                         
                            <?php endif; ?>

                        </div><!-- end div#content-cta -->

                    <?php elseif (get_row_layout() == 'testimonial_slider'): ?>

                    <?php content_module_close(); ?>

                        <?php if (have_rows('testimonials')): ?>
                            <div id="testimonial-slider" class="owl-carousel">

                            <?php while (have_rows('testimonials')): the_row(); ?>
                                <div class="testimonial">
                                    <div class="testimonial-content">
                                        <div class="testimonial-icon">
                                            <i class="fas fa-quote-left"></i>
                                        </div>
                                        <p class="description"><?php the_sub_field('testimonial'); ?></p>
                                    </div>
                                    <h3 class="title"><?php the_sub_field('author'); ?></h3>
                                    <span class="post"><?php the_sub_field('role'); ?></span>
                                </div>
                            <?php endwhile; ?>

                        </div>
                    
                    <?php endif; //testimonials ?>
        
                <?php endif; // get_row_layout ?>

            <?php endwhile; // have_rows?>

        <?php else: ?>
            <div class="content-module">
                <?php the_content(); ?>         
            </div><!-- end div.content-module -->
        <?php endif; ?>

This will open the wrapper with the content_module_open() function, if it's not aleady open, for each element in the Flexible Content loop. And it closes it before the CTA and Testimonials..