Bootstrap Modal and AFC Repeater field

2019-08-11 11:19发布

I have a client logo area on my website coming from an Advanced Custom Fields repeater, when clicked, I would like a modal window to pop up with the rest of the info I included in the repeater fields.

Each client has a logo, title, case study and testimonial. I have managed to get the logos to display in a nice grid, but I'm having some trouble with the modal popping up on click...

Here's my code:

    <div style="background-color:#ffffff;width:100%;">
    <div class="container margin-top-20" >

        <div class="row">
            <?php if( get_field('client_logos') ): ?>
            <div style="clear:both;margin-top:20px;">
            </div>
            <h3 class="brand-white" >
                Our Clients
            </h3>
            <ul class="blocks blog-block logo-block">
                <?php if( get_field('client_logos') ): ?>
                <?php while( has_sub_field('client_logos') ):  $i=1; ?>

                  <li>


                    <div class="block-image">
                        <div class="logo-image">
                            <div class="logo-center">
                                <?php $logoblock = get_sub_field('client_logo'); ?>
                                <a href="#myModal<?php echo $i;?>" data-toggle="modal" data-target="#myModal<?php echo $i;?>">
                                  <img src="<?php echo $logoblock['sizes']['medium']; ?>">
                                </a>
                            </div>
                        </div>
                    </div>


                    <div class="modal fade col-md-4" id="myModal<?php echo $i;?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog" >
                            <div class="modal-content" style="background-color:
                                <?php the_sub_field('box_color'); ?>">
                                <div class="modal-header">
                                    <!-- Close x --> 
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                    <!-- Logo --> 
                                    <?php the_field('client_logo');?>
                                    <!-- Title --> 
                                    <?php the_field('client_title');?>
                                </div>
                                <div class="modal-body">
                                    <!-- Case Study -->
                                    <?php the_field('client_case_study');?>
                                    <!-- Testimonial -->
                                    <?php the_field('client_testimonial');?>
                                </div>
                            </div>
                        </div>
                    </div>


                  </li>

                <?php $i++; endwhile; ?>
                <?php endif; ?>
            </ul>
            <?php else: ?>
            <?php endif; ?>
        </div>

    </div>
</div>

As you can see, the loop initially checks for the logo and displays it but it should display a modal window on click containing all of the fields... nothing seems to be happening when I click - I can't see what's wrong...

Here's a picture of the logo strip if it helps:

enter image description here

1条回答
祖国的老花朵
2楼-- · 2019-08-11 11:58

This is easy enough when I took a look at your code after seeing the live example.

#myModal'.$i.' - The variable isn't wrapped in PHP so the count doesn't display.

Change it up (and the modal div accordingly) to <a href="#myModal<?php echo $i;?>" data-toggle="modal" data-target="#myModal<?php echo $i;?>"><img src="<?php echo $logoblock['sizes']['medium']; ?>"></a>

The markup for the counts looks wrong... try this:

<div style="background-color:#ffffff;width:100%;">
<div class="container margin-top-20" >

    <div class="row">
        <?php if( get_field('client_logos') ): ?>
        <div style="clear:both;margin-top:20px;">
        </div>
        <h3 class="brand-white" >
            Our Clients
        </h3>
        <ul class="blocks blog-block logo-block">
            <?php if( get_field('client_logos') ): $i = 1; ?>
            <?php while( has_sub_field('client_logos') ): $i ++;?>

              <li>


                <div class="block-image">
                    <div class="logo-image">
                        <div class="logo-center">
                            <?php $logoblock = get_sub_field('client_logo'); ?>
                            <a href="#myModal<?php echo $i;?>" data-toggle="modal" data-target="#myModal<?php echo $i;?>">
                              <img src="<?php echo $logoblock['sizes']['medium']; ?>">
                            </a>
                        </div>
                    </div>
                </div>


                <div class="modal fade col-md-4" id="myModal<?php echo $i;?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog" >
                        <div class="modal-content" style="background-color:
                            <?php the_sub_field('box_color'); ?>">
                            <div class="modal-header">
                                <!-- Close x --> 
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                <!-- Logo --> 
                                <?php the_field('client_logo');?>
                                <!-- Title --> 
                                <?php the_field('client_title');?>
                            </div>
                            <div class="modal-body">
                                <!-- Case Study -->
                                <?php the_field('client_case_study');?>
                                <!-- Testimonial -->
                                <?php the_field('client_testimonial');?>
                            </div>
                        </div>
                    </div>
                </div>


              </li>

            <?php endwhile; ?>
            <?php endif; ?>
        </ul>
        <?php else: ?>
        <?php endif; ?>
    </div>

</div>

查看更多
登录 后发表回答