当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Wordpress ACF add div even

问题描述:

I have been given the code below which uses Wordpress Advanced Custom Fields to create boxes on a page. I am looking to include a blank div before every even div produced e.g:

<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. I think this could be done with PHP or 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:

<?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>';
}
?>
分享给朋友:
您可能感兴趣的文章:
随机阅读: