我已经获得了下面的代码,该代码使用Wordpress高级自定义字段在页面上创建框。我希望在每个偶数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
我下面的当前代码是生成框,我似乎无法弄清楚如何计算div并输入空白div。我认为这可以通过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>';
}?>
答案 0 :(得分:1)
您可以使用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>';
}
?>