I have a Post Type in WP and a Menu with the list of the posts like this:
<ul class="menu2">
<?php
$query = new WP_Query( array( 'post_type' => array( 'comisarios' ) ) );
while ( $query->have_posts() ) : $query->the_post();
echo '<li ';
echo '><a href="';
the_permalink();
echo '">';
the_title();
echo '</a></li>';
endwhile;?>
</ul>
I wish to know how I can select the current post to add a text like 'class="selected"', to show it cheked in black or something.
Thanks!
<ul class="menu2">
<?php
$postId = get_the_ID();
$query = new WP_Query( array( 'post_type' => array( 'comisarios' ) ) );
while ( $query->have_posts() ) : $query->the_post();
$idPost = get_the_ID();
echo '<li ';
if( $idPost==$postId) echo 'class="selected"';
echo '><a href="';
the_permalink();
echo '">';
the_title();
echo '</a></li>';
endwhile;?>
</ul>
It is a sidebar menu in the single-post.php template (the archive-post template is irrelevant in this case).
Here the complete code:
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="main">
<div class="boxCenter">
<div class="content"><!-- CONTENT -->
<div class="mainContent">
<div class="entry">
<div class="textBoxSmall">
<div class="comisario">
<img src="<?php the_field('imagen_del_comisario'); ?>">
</div>
<div class="textSmall comisarioText">
<h1><?php echo get_the_title(); ?></h1>
<p><?php the_field('res_comisario'); ?></p>
<a href="http://<?php the_field('web_del_comisario'); ?>"><?php the_field('web_del_comisario'); ?></a>
</p>
<p>Exposiciones:</p>
<div class="exposiciones">
<?php while(have_rows('exposiciones_del_comisario')): the_row(); ?>
<?php
$contador++;
?>
<div class="square box<?php echo $contador; ?>">
<div class="center">
<a href="<?php the_sub_field('exposicion'); ?>">
<?php echo $contador; ?>
</a>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
<div class="textBox">
<?php the_content();?>
</div>
</div>
</div>
</div><!-- CONTENT END -->
<?php endwhile; // end of the loop. ?>
<div class="sidebar hide"><!-- LATERAL MENU -->
<ul class="menu2">
<?php
$query = new WP_Query( array( 'post_type' => array( 'comisarios' ) ) );
while ( $query->have_posts() ) : $query->the_post();
echo '<li ';
echo '><a href="';
the_permalink();
echo '">';
the_title();
echo '</a></li>';
endwhile;?>
</ul>
</div><!-- END LATERAL MENU -->