I am trying to do something I have not seen before in wordpress. Basically, when you arrive at the blog, a title, thumbnail and an excerpt is displayed. When a toggle button is pushed, the post should slide down to reveal the content. (<?php the_content(); ?>
)
Here is my Jquery:
$(document).ready(function() {
$('span.play a').click(function() {
if ($('.toggleSection').is(":hidden"))
{
$('.toggleSection').slideDown("slow");
} else {
$('.toggleSection').slideUp("slow");
}
return false;
});
});
It works perfectly! However; because the toggle is placed within the wordpress loop, whenever the toggle button is pressed, all of the posts toggle open. For instance, If I have 10 posts on a page, and one toggle button is clicked, all of the toggles open. Here is my WP loop:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div class="post" id="<?php the_ID(); ?>">
<div class="thumbnail">
<?php the_post_thumbnail( 'mainimg' ); ?>
<span class="play"><a href="#" onclick="jQuery('#comments').toggle();"><img src="<?php echo bloginfo('template_url'); ?>/images/play.png" width="30" height="36" alt="Play Video"></a></span>
</div>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<h3>Posted in: <span><?php the_category(', ') ?> </span>On: <span><?php the_time('l, F jS, Y') ?></span></h3>
<div class="entry">
<p><?php the_excerpt(); ?> </p>
</div> <!-- entry -->
<div class="toggleSection">
<p><?php the_content(); ?> </p>
</div>
</div> <!-- .post -->
<?php endwhile; ?>
What you are seeing above, is that when span.play a is clicked, the togglesection slides down and reveals the content. When any single toggle is selected, all of the content appears. I would like it so each toggle is unique within the WP loop, and only reveals that entry’s content. Any ideas?
You can see a demo of my work so far here: http://vitaminjdesign.com/littlewindow/ Press the play button over the thumbnails to toggle!
You can slim down your current code and fix the issue with toggling all of them like this:
This goes up to the
.post
element using.closest()
then does a.find()
to get the.toggleSection
only inside that.post
. Then the toggle code can be condensed down to just a.slideToggle()
call. The above centers around using the current element,$(this)
, then traversing to find the element(s) you’re after using the tree traversal functions.