Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a video viewing section on my site. The single post template displays a large area where the video plays and then there's a column of thumbnails representing other videos along the right side. Much like YouTube.

If the current video that is being displayed (let's say the post ID is 123) matches one of the thumbnail on the right (also ID 123) I want to give the thumbnail a different css class (represented by the inline style below) so I can highlight it, letting the user know this is the video currently playing.

I'm using wp_query to grab the posts for the right sidebar portion and I can't figure out how to compare the two IDs while in the loop. Below is a simplified version of what I thought would work, but it just gives the class to all iterations of the loop instead of just the current one.

<?php while ( have_posts() ) : the_post(); 
$primaryid = get_the_ID(); ?>
<?php 
            $args = array (
                'post_type' => 'post',
                'post_status' => 'publish',
                'orderby' => 'meta_value_number',
                'meta_key' => 'episode',
                'order' => 'ASC',
                );
            // The Query
            $query = new WP_Query( $args );

            // The Loop
            if ( $query->have_posts() ) { ?>

                <?php while ( $query->have_posts() ) {
                    $query->the_post(); ?>
                    <?php if (get_the_ID() == $primaryid) {
                        $style = 'style="border: 1px solid #fff;"';
                        } ?>
<?php endwhile; // end of the loop. ?>
share|improve this question
add comment

1 Answer

Figured it out, but not sure if it's the best way or not. I got the id of the current post in the loop and then used is_single to figure out which post was being displayed, then gave if the styling. The problem was, this was also carrying over to the next itteration of the loop and altering that styling as well. To fix that I just rest the $style variable to null at the end of the loop.

$curID = get_the_ID(); 
                     if (is_single($curID) == true) {
                        $style = 'style="border: 1px solid #fff;"';
                        } 
//Stuff here?>

<?php $style = ''; ?>
share|improve this answer
1  
You could make this a little bit neater with a ternary operator. –  s_ha_dum Aug 23 '13 at 14:02
    
Thanks I'll check it out! –  Eckstein Aug 23 '13 at 18:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.