WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer
I just had to change 'post_type' => 'post' to 'post_type' => 'movie' and it worked great. Thanks a lot. – gil hamer Oct 26 '13 at 20:03
Yep. I changed that parameter for testing and didn't change it back. Sorry. – s_ha_dum Oct 26 '13 at 20:08
Great answer. worked great for me. Thanks! – gil hamer Oct 26 '13 at 20:15

Your Answer


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.