• Resolved tjsherrill

    (@tjsherrill)


    <?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)
  • 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

    (@tjsherrill)

    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.

    try:
    <li <?php $class = ($current_id == $post->ID) ? ' class="current_page' : ''; echo $class; ?>>

    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

    (@tjsherrill)

    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

    (@tjsherrill)

    Mark that was exactly what I needed. Thank you for your help.

    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.