简体   繁体   English

Wordpress ACF 甚至添加 div

[英]Wordpress ACF add div even

I have been given the code below which uses Wordpress Advanced Custom Fields to create boxes on a page.我得到了下面的代码,它使用 Wordpress 高级自定义字段在页面上创建框。 I am looking to include a blank div before every even div produced eg:我希望在生成每个偶数 div 之前包含一个空白 div,例如:

<div class="span2 team-'. $count .'"> </div>
<div class="span2 team-blank even"> </div> <- inserted blank div needed
<div class="span2 team-'. $count .'"> </div>
<div class="span2 team-blank even"> </div> <- inserted blank div needed

My current code below is producing the boxes, which I can't seem to work out how to count the div and input the blank div.我下面的当前代码正在生成框,我似乎无法弄清楚如何计算 div 并输入空白 div。 I think this could be done with PHP or Jquery.我认为这可以用 PHP 或 Jquery 来完成。

                    <?php
if ( get_field('team') )
{
echo '<div class="span6">';
echo '<div class="row">';
$count=0;
while ( has_sub_field('team') )
{
    echo '<div class="span2 team-'. $count .'"> ';
    echo '<a class="inline cboxElement" href="#inline_content-'. $count .'">';
    $personimage_url = wp_get_attachment_image_src(get_sub_field('image_person'), 'why_img');
    echo '<img src="' . $personimage_url[0] . '">';
    echo '</a> ';
    echo '<div style="display:none"> ';
    echo '<div id="inline_content-'. $count .'" class="row"> ';
     echo '<div class="span10 about-area">';
     echo '<div class="contact_img">';
    $about_url =         wp_get_attachment_image_src(get_sub_field('image_person'), 'about_img');
    echo '<img src="' . $about_url[0] . '"></div>';
    if ( get_sub_field('name') ) echo '<span class="name">' .         get_sub_field('name') . '</span> ';
    if ( get_sub_field('about') ) echo '<span class="about">'.     get_sub_field('about') .'</span>';
    echo '</div><br style="clear: both;">';
    echo '<div id="lightbox">
<h1 id="site-title" class="span5"> <span> <a href="http://www.website.com" title="Website" rel="home">Website</a>         </span> </h1>
</div>';
    echo '</div> ';
    echo '</div> ';
    echo '<span class="contact_label">' . get_sub_field('name') .     '</span>';
    echo '</div> ';
    $count++;
}
echo '</div>';
echo '</div>';
}?>

You could use the modulus operator along with your count variable to do this:您可以使用modulus运算符和count变量来执行此操作:

<?php
if ( get_field('team') )
{
echo '<div class="span6">';
echo '<div class="row">';
$count=0;
while ( has_sub_field('team') )
{

    echo '<div class="span2 team-'. $count .'"> ';
    echo '<a class="inline cboxElement" href="#inline_content-'. $count .'">';
    $personimage_url = wp_get_attachment_image_src(get_sub_field('image_person'), 'why_img');
    echo '<img src="' . $personimage_url[0] . '">';
    echo '</a> ';
    echo '<div style="display:none"> ';
    echo '<div id="inline_content-'. $count .'" class="row"> ';
     echo '<div class="span10 about-area">';
     echo '<div class="contact_img">';
    $about_url =         wp_get_attachment_image_src(get_sub_field('image_person'), 'about_img');
    echo '<img src="' . $about_url[0] . '"></div>';
    if ( get_sub_field('name') ) echo '<span class="name">' .         get_sub_field('name') . '</span> ';
    if ( get_sub_field('about') ) echo '<span class="about">'.     get_sub_field('about') .'</span>';
    echo '</div><br style="clear: both;">';
    echo '<div id="lightbox">
<h1 id="site-title" class="span5"> <span> <a href="http://www.website.com" title="Website" rel="home">Website</a>         </span> </h1>
</div>';
    echo '</div> ';
    echo '</div> ';
    echo '<span class="contact_label">' . get_sub_field('name') .     '</span>';
    echo '</div> ';

    // Use modulo to see if $count is divisible by 2 (i.e. an even number)
    if ($count % 2 === 0)
    {
        // Insert blank div
        echo '<div class="span2 team-blank even"> </div>';
    }

    $count++;
}
echo '</div>';
echo '</div>';
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM