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(); ?>
<li><?php
if ($thispage === $post->ID) {
$class=" class="current-item"";
} else {
$class="";
}
the_title(
'<a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ). '"' . $class.' >',
'</a>'
); ?>
</li><?php
endwhile;
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.