• Resolved leandrodpzla

    (@leandrodpzla)


    I have the following code. It’s a Menu built with a custom loop of sub-pages of a custom post type. All I wanna do is add a class “selected” if the current page is showing:

    <ul class="menu3">
    					  	<li><a href="<?php echo qtrans_convertURL(get_permalink($post->post_parent), qtrans_getLanguage()); ?>" class="<?php if  (is_single($post->post_parent)) { echo 'selected'; } ?>"><?php _e('[:en]Overview[:es]Resumen[:pt]Vis?o Global'); ?></a></li>
    						<?php $parent = $post->ID;
    							  $chil = $post->post_parent;
    							?>
    						<?php if ( $post->post_parent ) {?>
    					  	<?php query_posts('post_type=projects&orderby=menu_order&order=ASC&post_parent='.$chil);
    						?>
    					  	<?php } else {
    							query_posts('post_type=projects&orderby=menu_order&order=ASC&post_parent='.$parent);
    					  	}
    					  	while (have_posts()) : the_post ();
    					  	?>
    						<li><a href="<?php echo qtrans_convertURL(get_permalink(), qtrans_getLanguage()); ?>" class="<?php if ( is_single( get_the_ID() ) ) { echo 'selected';} ?>"><?php echo the_title(); ?></a></li>
    						<?php endwhile; ?>
    
    					</ul>

    But the conditional tag, appears to not working at all. Exactly this part of the code isnide the loop:

    class="<?php if ( is_single( get_the_ID() ) ) { echo 'selected';} ?>"

Viewing 2 replies - 1 through 2 (of 2 total)
  • I think it might be better to save the current ID before beginning the code for the menu and then compare each post ID to the saved one.

    Thread Starter leandrodpzla

    (@leandrodpzla)

    After have been reading for a while I figured out that I should use Wp_Query instead query_posts. I tested and works like a charm.

    <?php
    							$parent = $post->ID;
    							$chil = $post->post_parent;
    						?>
    						<?php if ( $post->post_parent ) {?>
    					  	<?php
    					  		$my_posts = new WP_Query('post_type=projects&orderby=menu_order&order=ASC&post_parent='.$chil);
    						?>
    					  	<?php
    							} else {
    					  		$my_posts = new WP_Query('post_type=projects&orderby=menu_order&order=ASC&post_parent='.$parent);
    					  	}?>
    					  	<?php if ($my_posts->have_posts()) : ?>
        					<?php while ($my_posts->have_posts()) : $my_posts->the_post(); ?>
    
    						<li><a href="<?php echo qtrans_convertURL(get_permalink(), qtrans_getLanguage()); ?>" class="<?php if ( is_single($post->ID) ) { echo 'selected'; } ?>"><?php echo the_title(); ?></a></li>
    
    						<?php endwhile; ?>
    						<?php endif; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Conditional tags not working inside the loop’ is closed to new replies.