用div包装整套动态内容

时间:2014-11-08 19:54:14

标签: php wordpress advanced-custom-fields

我是一个PHP新手,我无法弄清楚我的PHP中的一些代码放在哪里将输出的内容包装在div中。就目前而言,这会输出两次回声,每次都围绕“season_date”行和“archive_entry”行。我想将整个事情包装在一个div中。

这可能是非常明显的事情,但我似乎无法弄清楚我做错了什么......

以下是代码:

<?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?

    }
} ?>

(如果重要的话,我正在使用Wordpress的高级自定义字段插件以及灵活的内容和转发器内容附加组件。)

2 个答案:

答案 0 :(得分:2)

尝试使用此代码,每次找到新赛季时都会打开一个div:

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

答案 1 :(得分:0)

我遇到了类似的问题,我需要在“模块化”样式页面中显示灵活内容字段 - 例如,页面内容在内容部分周围有一个包装器,但号召性用语和推荐滑块需要关闭该包装器在它开始之前并在之后打开它......所以只有行动呼吁和推荐是全宽的。

这是我想出的,不过,可能有一种更优雅的编码方式..这行得通:)

                    <?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; ?>

这将使用 content_module_open() 函数打开包装器,如果它没有打开,则为灵活内容循环中的每个元素。它在 CTA 和推荐之前关闭它..