Title: Adding a .current class to a query_posts array
Last modified: August 19, 2016

---

# Adding a .current class to a query_posts array

 *  Resolved [tjsherrill](https://wordpress.org/support/users/tjsherrill/)
 * (@tjsherrill)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/adding-a-current-class-to-a-query_posts-array/)
 *     ```
       <?php query_posts(array('post_parent' => 6, 'post_type' => 'page')); while (have_posts()) { the_post();
       				$current_id = $post->ID; ?>
       				<li <?php $class = ($current_id == $post->ID) ? ' class="current_page' : '';?>>
   
                       		<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('align=left'); ?>
                           	<span class="products_title"><?php the_title(); ?></span>
                           	<span class="products_arrow">></span></a>
   
       				</li>
       		    <?php } ?>
       ```
   
 * This is what I currently have. and clearly something is wrong with it. I want
   to add class .current_page to the current page. Is this possible, am I just missing
   something dumb?

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/adding-a-current-class-to-a-query_posts-array/#post-1629478)
 * shouldn’t there be an `echo` in the code?
 * btw: it would add the class ‘.current_page’ to all pages in the loop.
 *  Thread Starter [tjsherrill](https://wordpress.org/support/users/tjsherrill/)
 * (@tjsherrill)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/adding-a-current-class-to-a-query_posts-array/#post-1629584)
 * Maybe, I am newer to php and WP. I can typically figure these things out slowly
   but surely but this one has been giving me fits. I assume by echo you mean that
   somewhere in there I should say echo “if its the current page put class=”current_page”.
 * At the end of the day I only want the .current_page on the current_page, and 
   I dont think I can use wp_list_pages for this as I am grabbing different pieces
   of content from the page.
 * any more help is greatly appreciated.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/adding-a-current-class-to-a-query_posts-array/#post-1629589)
 * try:
    `<li <?php $class = ($current_id == $post->ID) ? ' class="current_page':'';
   echo $class; ?>>`
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/adding-a-current-class-to-a-query_posts-array/#post-1629616)
 * Check if one of the query vars is set prior to the loop, then check against that
   value in the post loop.
 * **Example:**
 *     ```
       <?php
       // Store queried page name if available (should be available for pretty permalinks)
       $_queried_page = ( get_query_var('pagename') ) ? get_query_var('pagename') : false;
       // Store queried page id if available (fallback if pagename failed)
       $_queried_page_id = ( get_query_var('page_id') ) ? get_query_var('page_id') : false;
   
       query_posts( array(
       	'post_parent' => 6,
       	'post_type' => 'page'
       ) ); 
   
       while (have_posts()) :
       	the_post();
   
       	// Check if the post name is the queried page
       	if( $_queried_page && ( $_queried_page == $post->post_name ) )
       		$append = ' class="current_page';
       	// If pagename not set, check id
       	elseif( $_queried_page_id && ( $_queried_page_id == $post->ID ) )
       		$append = ' class="current_page';
       	// Else not current
       	else
       		$append = '';
       ?>
   
       	<li<?php echo $append; ?>>
       		<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('align=left'); ?>
       			<span class="products_title"><?php the_title(); ?></span>
       			<span class="products_arrow"></span>
       		</a>
       	</li>
   
       <?php
       endwhile;
       ?>
       ```
   
 * Hope that helps…
 *  Thread Starter [tjsherrill](https://wordpress.org/support/users/tjsherrill/)
 * (@tjsherrill)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/adding-a-current-class-to-a-query_posts-array/#post-1629624)
 * Thanks so much for all this help. I will work to implement and see how it goes.
   Then I’ll report back… thanks
 *  Thread Starter [tjsherrill](https://wordpress.org/support/users/tjsherrill/)
 * (@tjsherrill)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/adding-a-current-class-to-a-query_posts-array/#post-1629663)
 * Mark that was exactly what I needed. Thank you for your help.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/adding-a-current-class-to-a-query_posts-array/#post-1629781)
 * You’re welcome.. 🙂

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Adding a .current class to a query_posts array’ is closed to new replies.

## Tags

 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 3 participants
 * Last reply from: [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * Last activity: [15 years, 10 months ago](https://wordpress.org/support/topic/adding-a-current-class-to-a-query_posts-array/#post-1629781)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
