Jump to content

Recommended Posts

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

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

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

  • Like 1
Link to comment
Share on other sites

  • 5 months later...

thank you 

BernhardB

that 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
}
?>
  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...