【问题标题】:PHP BootStrap GridPHP 引导网格
【发布时间】:2017-02-17 16:21:18
【问题描述】:

我正在制作一个 WordPress 主题并使用 Advanced Custom Fields 我添加了一个 repeater option

现在的问题是我想允许用户添加任意​​数量的“服务”,但我不知道如何做到这一点,以便根据服务数量动态创建引导网格。

我看到了这个问题:Using PHP loop to add Bootstrap rows and proper column numbers to elements 但无法弄清楚如何正确使用它来满足我的需要。

我尝试的是计算服务的数量并从该函数返回 html。

然后我创建了一个str_replace_first_occurance 函数,它基本上在第一次出现“内容”时执行了str_replace。它通过这个循环了有服务的数量。

我想这应该可行,但它变得非常混乱,似乎不是最好的方法。

实现这一目标的最佳方法是什么?

【问题讨论】:

  • 为什么投反对票?有什么解决办法吗?

标签: php wordpress twitter-bootstrap advanced-custom-fields


【解决方案1】:

我使用了这段代码,效果很好!

    <?php

    $max_columns = 3; //columns will arrange to any number (as long as it is evenly divisible by 12)
    $column = 12/$max_columns; //column number
    $total_items = count( get_field('services_services'));
    $remainder = $total_items%$max_columns; //how many items are in the last row
    $first_row_item = ($total_items - $remainder); //first item in the last row
    ?>

    <?php $i=0; // counter ?>

    <?php while ( have_rows('services_services') ) : the_row(); ?> 

        <?php if ($i%$max_columns==0) { // if counter is multiple of 3 ?>
        <div class="row">
        <?php } ?>

        <?php if ($i >= $first_row_item) { //if in last row ?>   
        <div class="col-md-<?php echo 12/$remainder; ?>">
        <?php } else { ?>
        <div class="col-md-<?php echo $column; ?>">
        <?php } ?>

            <div class="service-box">
                <h3 class="service-heading"><?php echo the_sub_field('service_name'); ?></h3>
                <span class="<?php echo the_sub_field('service_fa_class'); ?>" aria-hidden="true"></span>
                <p class="heading-text"><?php echo the_sub_field('service_description'); ?></p>
                <a href="<?php echo the_sub_field('service_learn_more_url'); ?>" class="btn btn-info">Learn More</a>        
            </div>

        </div>        

        <?php $i++; ?>

        <?php if($i%$max_columns==0) { // if counter is multiple of 3 ?>
        </div>
        <?php } ?>

    <?php endwhile; ?>

    <?php if($i%$max_columns!=0) { // put closing div if loop is not exactly a multiple of 3 ?>
    </div>
    <?php } ?>

【讨论】:

    【解决方案2】:

    您希望每行显示多少?您很可能希望通过 ACF 中继器提供的数组运行 foreach。然后把你的价值放在下面。我假设您知道如何将 php 放入您的前端?

        <div class="col-lg-4">
            // Content which is your service
        </div>
    

    您还可以从转发器字段中获取数组的计数,以确定添加了多少服务。然后你可以做一些逻辑来确定你想要它是 col-lg-3 还是 col-lg-4。

    【讨论】:

    • 如果你给我看 var_dump 并死在你的 ACF 字段上,我可以给你确切的指示:$value = get_field("your_field_name");
    猜你喜欢
    • 2016-04-27
    • 2017-01-29
    • 2016-12-13
    • 2018-06-23
    • 1970-01-01
    • 2016-03-11
    • 2014-08-11
    • 2014-07-04
    相关资源
    最近更新 更多