I have a custom post type called: "movie"
I have only 4 movies in my movies section in the site.
Inside each movie post type I want to display all other movies in a side list like so:

  • movie 1
  • movie 2
  • movie 3
  • movie 4

so the user can navigate from each movie page to all other movies.

I used this code to display the list of all of my "movie" post types:

<?php $loop = new WP_Query( array( 'post_type' => 'movie', 'posts_per_page' => 4, 'order'=>'ASC' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php the_title( '<a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '">', '</a>' ); ?>
    <?php endwhile; ?>

So far it is very simple and working.

I would also like to add a "current" class to each item in the list that corresponds to the post I am currently reading. for instance if I am inside movie 3 I see this:

  • movie 1
  • movie 2
  • movie 3
  • movie 4

The problem is that unlike WordPress custom menu, no "current-menu-item" or other class is automatically generated.

any clue how I can accomplish that?

1 Answer 1


I am assuming that your code is on a "single" post/cpt page.

echo '<ul>';
  $loop = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => 4, 'order'=>'ASC' ) );
  $obj = get_queried_object();
  $thispage = $obj->ID;
  while ( $loop->have_posts() ) : $loop->the_post(); ?>
      if ($thispage === $post->ID) {
        $class = ' class="current-item"';
      } else {
        $class = '';
        '<a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ). '"' . $class.' >', 
      ); ?>
echo '</ul>';

You want to use get_queried_object to get data about the page you are on and compare the ID to the items in the loop, adding a class attribute if they match. This will not work correctly on archive pages as get_queried_object will not return the same type of data on those pages. Depending on your implementation, you may have to alter the code.

  • I just had to change 'post_type' => 'post' to 'post_type' => 'movie' and it worked great. Thanks a lot.
    – gil hamer
    Oct 26, 2013 at 20:03
  • Yep. I changed that parameter for testing and didn't change it back. Sorry.
    – s_ha_dum
    Oct 26, 2013 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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