我尝试将first
和last
类添加到某些LI
元素中,并且我遇到了第一个索引的问题。我遇到问题的代码在这里:
// this is always true, and all items get the `first` class
if ($i == 0) { $menu_item_class .= ' first'; }
整个代码如下所示:
function social_icons( $menu_item_class='', $icon_class='', $title_class = ''){
$menu_item_class .= ($title_class != '') ? ' no-title' : '';
$social_network = array(
0=> array (
"title"=> "ello",
"url"=>"envato"
),
1=> array (
"title"=> "vk",
"url"=> "envato"
),
2=> array (
"title"=> "twitter",
"url"=> "envato"
),
3=> array (
"title"=> "lastfm",
"url"=> "envato"
)
);
$social_icon_wrapper = ' string';
$type = ' string2';
$count = 4;
if ( $count > 0 ) { ?>
<div id="social-network">
<ul class="social-icons">
<?php foreach ($social_network as $i=>$sn ){
// var_dump($i); // this always returns fine
if ( $i == 0 ) { $menu_item_class .= ' first'; } // for some reason this IF is always true
if ( $i == ($count-1) ) { $menu_item_class .= ' last'; } ?>
<?php if ( $sn['title'] == 'ello' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="ello" href="https://ello.co/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-ello"></i></span>ello</a></li><?php } ?>
<?php if ( $sn['title'] == 'vk' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="vk" href="https://vk.com/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-vk"></i></span>vk</a></li><?php } ?>
<?php if ( $sn['title'] == 'facebook' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="facebook" href="https://www.facebook.com/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-facebook"></i></span>facebook</a></li><?php } ?>
<?php if ( $sn['title'] == 'pinterest' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="pinterest" href="https://pinterest.com/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-pinterest"></i></span>pinterest</a></li><?php } ?>
<?php if ( $sn['title'] == 'twitter' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="twitter" href="https://www.twitter.com/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-twitter"></i></span>twitter</a></li><?php } ?>
<?php if ( $sn['title'] == 'lastfm' ) { ?> <li class="<?php echo $menu_item_class; ?>"><a class="lastfm" href="http://www.last.fm/user/<?php echo $sn['url']; ?>"><span class="social-icon-wrapper<?php echo $type; ?>"><i class="<?php echo $icon_class; ?> social-lastfm"></i></span>lastfm</a></li><?php } ?>
<?php } ?>
</ul>
</div>
<?php }
}
social_icons();
我做了一个简单的pen来说明问题。请检查并让我知道错误。
答案 0 :(得分:1)
这样你就可以理解,在this code采取行动。
更多解释:一旦变为$class_name
并且abc
IS等于{,您有一个变量foreach
,例如值$i
{1}},0
将是$class_name
。但是从现在开始,您将继续访问abc first
,即猜猜是什么,$class_name
。
This Code已更改为保留正确的值(请注意,这可能不是最好的方法,但您需要了解的是,一旦您将abc first
添加到变量,下次访问时它,它会在那里。)
查看first
行:
// ADDED THIS LINE
答案 1 :(得分:0)
试试这个
$class_name ='';
if ($i == 0) { $class_name = ' first'; }