Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Boris Kamp
Boris Kamp
16,660 Points

Dynamically add new div id to js activation call

Hi guys!

Im using this great carousel in my website: http://owlgraphic.com/owlcarousel/ It works great!

On my project archive page I use this code to generate owl carousels for each project:

<?php if ( is_post_type_archive( 'projecten' )){ ?>
          <div id="owl-<?php echo $post->ID; ?>" class="col-xs-12 no-lr-padding owl-carousel">
            <?php if( get_field('foto_galerij') ): ?>
            <?php $images = get_field('foto_galerij');
            if( $images ): ?>
            <?php foreach( $images as $image ): ?>
              <div class="foto-wrapper">
                <a href="<?php echo $image['url']; ?>" data-gallery>
                  <img class="img-responsive img-rounded" src="<?php echo $image['sizes']['project-slider']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['title']; ?>" />
                </a>
                </div>
            <?php endforeach; ?>
            <?php endif; ?>
            <?php endif; ?>
          </div>
          <span class="glyphicon glyphicon-chevron-left"></span>
          <span class="glyphicon glyphicon-chevron-right"></span>
        <?php } ?>

note the <div id="owl-<?php echo $post->ID; ?>" part where the loop echos out the post id in the div ID. I need this because each carousel must be unique as far as I know.

Next, in my theme.js file I call the carousels:

var projecten = $("#owl-68,#owl-69,#owl-70,#owl-71,#owl-72,#owl-73");

It works, but when I add an extra project, it appears in my archive and I need to manually add this project's ID to the var above in order for it to work on that project.

Is there a way to automate this somehow?

1 Answer

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Can you apply a class to that same div and call everything based on the class?

Boris Kamp
Boris Kamp
16,660 Points

Stupid! I thought I could only call it by ID's........ shaming myself right now, haha. Thanks Zac!

Zac Gordon
Zac Gordon
Treehouse Guest Teacher

;p sometimes just takes a second set of eyes ;)

Boris Kamp
Boris Kamp
16,660 Points

Haha you're right! Thanks!