每个转发器字段的唯一ID(高级自定义字段)

时间:2016-10-27 14:24:53

标签: php wordpress counter advanced-custom-fields

我正在创建具有节目时间表的wordpress页面。我使用高级自定义字段,以便我们可以轻松地填充当天的部件并移除等等。但是每个块都有自己的可折叠描述和href。当我创建转发器时,它保持该可折叠部分的id相同并且它不起作用因为它将始终打开'description 1'

我的问题:如何将唯一ID(即progam1,program2等)应用于每个转发器循环。我已经尝试使用$ counter,如下面的代码所示,但我不确定如何正确应用它。

<?php 
while ( have_rows('day_1') ) : the_row();

    $progtime             = get_sub_field('prog_time');
    $progtitle            = get_sub_field('prog_title');
    $progimage           = get_sub_field('prog_image');
    $progsubspeaker     = get_sub_field('prog_sub_speaker');
    $progsubdescription = get_sub_field('prog_sub_description');
    $progsubduration    = get_sub_field('prog_sub_duration');
    $progsublocation    = get_sub_field('prog_sub_location');
    $progsubabouttitle  = get_sub_field('prog_sub_abouttitle');
    $progsubabout       = get_sub_field('prog_sub_about');
    $progsubabouturl    = get_sub_field('prog_sub_abouturl');
    $counter = 1;

    ?>
    <!-- PROGRAM 1-->
    <div class="panel panel-default">

        <!-- Program Heading -->
        <div class="panel-heading" role="tab" id="heading1">

            <div class="row">
                <div class="col-lg-1 col-md-1 col-sm-1">
                    <p class="date"><?php echo $progtime; ?></p>
                </div>

                <div class="col-lg-11 col-md-11 col-sm-11">

                    <h4 class="panel-title">
                        <a data-toggle="collapse" data-parent="#accordion" href="#Program<?php echo $counter; ?>" aria-expanded="true" aria-controls="Program1">
                            <?php echo $progtitle; ?>
                        </a>
                    </h4>
                </div>
            </div>

        </div>

        <div id="Program<?php echo $counter; ?>" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading1">
            <!-- Program Content -->
            <div class="panel-body">
                <div class="row">
                    <div class="col-lg-2 col-md-2 col-sm-2">
                        <img class="img-responsive img-circle" src="<?php echo $progimage; ?>" alt="">
                    </div>

                    <div class="col-lg-7 col-md-7 col-sm-10">
                        <p class="speaker-name uppercase"><?php echo $progsubspeaker; ?></p>
                            <h4><?php echo $progtitle; ?></h4>
                                <p><?php echo $progsubdescription; ?></p>

                                <p><i class="fa fa-lg fa-clock-o"></i><span class="small"><?php echo $progsubduration; ?></span></p>
                                <p><i class="fa fa-lg fa-map-marker"></i> <span class="small"><?php echo $progsublocation; ?></span></p>
                            </div>

                            <div class="col-lg-3 col-md-3 col-sm-10">
                                <h5><?php echo $progsubabouttitle; ?></h5>
                                <p class="small"><?php echo $progsubabout; ?></p>
                                <span class="about-speaker"><i class="fa fa-lg fa-globe"></i> <a class="small" href="#"><?php echo  $progsubabouturl; ?></a></span>
                            </div>

                        </div>
                    </div>

                </div>

            </div>
            <?php $counter++; ?>
        <?php endwhile; ?> 

2 个答案:

答案 0 :(得分:0)

知道了。我使用this source on stackoverflow作为解决方案。

我必须在循环之前定义$ counter并在循环中添加$ counter ++,所以每次循环循环都会增加+1。

答案 1 :(得分:0)

你的问题是$ counter在循环中被定义为1,因此每次运行时它都会将其设置为等于1.

在while语句上方移动$ counter = 1,这应该适合你!