【问题标题】:Wrapping an entire set of dynamic content with a div用 div 包装整套动态内容
【发布时间】:2015-01-05 10:27:35
【问题描述】:

我是 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 的高级自定义字段插件以及灵活的内容和转发器内容插件。)

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    试试这段代码,每次发现新赛季时都会打开一个 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>';
    } ?>
    

    【讨论】:

    • 那是我最初的冲动,但最终用 div 包裹了整个输出。即:
      季节日期存档条目存档条目存档条目季节日期存档条目存档条目存档条目
      我想实现:
      季节日期存档条目存档条目存档条目
      季节日期归档条目归档条目归档条目
    • 抱歉,没有意识到我无法为回复评论添加格式。
    • 谢谢谢谢!是的!像魅力一样工作。你能向我解释一下为什么会这样吗?
    • 当然!您实际上想要实现的是将季节包装到 div 中。该循环为您提供两种对象:有时是季节,有时是存档条目。每次遇到新的季节,我们都需要启动 div。如果我们之前已经遇到过一个赛季,我们还想关闭包装上一个赛季的 div(因此使用 $first 变量)。最后,在循环结束时,我们需要关闭最后一个打开的季节。
    • 我已经编辑了代码来处理循环中没有找到季节的情况
    【解决方案2】:

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

    这就是我想出的,不过,可能有一种更优雅的方式来编写代码……这很有效:)

                        <?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 和推荐之前关闭它..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-10
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 2014-10-21
      • 1970-01-01
      相关资源
      最近更新 更多