Basil Posted October 10, 2014 Share Posted October 10, 2014 hi to Processwire LOVELY COMMUNITY! i have a question about how can i add a class to current viewable image from images field. my code is like this: <?php foreach($page->images as $img) { ?> <div class="item"> <img src="<?php echo $img->url; ?>" /> <div class="container"> <div class="carousel-text"> <h2><?php echo $img->description; ?></h2> </div><!-- /carousel-text --> </div><!-- /container --> </div><!-- /item --> <?php } ?> i need to add only for the current image to this div <div class="item"> the class of active, like this <div class="item active"> is there any way to make it happen? Thank you in advance! Link to comment Share on other sites More sharing options...
kongondo Posted October 10, 2014 Share Posted October 10, 2014 If I get you correctly, you want the current image displayed by your carousel? PW has no control over that. That will have to be done using JS I think.... Link to comment Share on other sites More sharing options...
Basil Posted October 10, 2014 Author Share Posted October 10, 2014 thank you kongondo! i manage to solve it this way: <!-- Wrapper for slides --> <div class="carousel-inner"> <?php $first = $page->images->first(); ?> <div class="item active"> <img src="<?php echo $first->url; ?>" /> <div class="container"> <div class="carousel-text"> <h2><?php echo $first->description; ?></h2> </div><!-- /carousel-text --> </div><!-- /container --> </div><!-- /item --> <?php foreach($page->images->slice(1) as $img) { ?> <div class="item"> <img src="<?php echo $img->url; ?>" /> <div class="container"> <div class="carousel-text"> <h2><?php echo $img->description; ?></h2> </div><!-- /carousel-text --> </div><!-- /container --> </div><!-- /item --> <?php } ?> </div><!-- /carousel-inner --> i dont know a better way, but i wait and see.. Link to comment Share on other sites More sharing options...
bernhard Posted October 11, 2014 Share Posted October 11, 2014 you mean you want to apply the "active" class only to the first item? maybe something like this <?php $active = ' active'; // set active for first item foreach($page->images as $img) { ?> <div class="item<?= $active ?>"> <img src="<?php echo $img->url; ?>" /> <div class="container"> <div class="carousel-text"> <h2><?php echo $img->description; ?></h2> </div><!-- /carousel-text --> </div><!-- /container --> </div><!-- /item --> <?php $active = ''; // remove class "active" for following items } ?> i like to use short tags <?= $var ?> instead of <?php echo $var; ?> maybe you have to change this depending on your server 1 Link to comment Share on other sites More sharing options...
Basil Posted April 8, 2015 Author Share Posted April 8, 2015 thank you BernhardBthat is exactly what i want! <?php $active = ' active'; // set active for first item foreach($page->images as $img) { ?> <div class="item<?= $active ?>"> <img src="<?php echo $img->url; ?>" /> <div class="container"> <div class="carousel-text"> <h2><?php echo $img->description; ?></h2> </div><!-- /carousel-text --> </div><!-- /container --> </div><!-- /item --> <?php $active = ''; // remove class "active" for following items } ?> 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now