0

I have a custom post type, "case". I'm currently editing the single-case.php file and in my sidebar I have the following piece of code:

<?php
$case_query = array (
    'post_type' => 'case',
    'showposts' => -1,
    'orderby' => 'title',
    'order' => ASC
);
query_posts($case_query);
if ( have_posts() ) {
    echo '<div class="case-list">';
    while (have_posts()) {
        the_post();
        $title = get_the_title();
        $url = get_permalink(); ?>
        <div class="case-list-item">
            <div class="ico"><i class="fa fa-fw fa-angle-right"></i></div>
            <div class="link"><a href="<?php echo $url; ?>"><?php echo $title; ?></a></div>
        </div>

        <?php
    }
    echo '</div>';
    wp_reset_query();
}
?>

What I'm wondering is, how do I check which of the posts in above while-loop is the currently viewed post and then add a class of "current" to it? Or am I doing this all kinds of wrong?..

1 Answer 1

0

I figured it out.

Before the declaration of $case_query, I declare the active page id, like so: $active_page = get_the_ID();. Then, in the while loop after the_post(); I add the following:

$activeclass = '';
if (get_the_ID() === $active_page) {
    $activeclass = 'current-page';
}

I also add the $activeclass to the .case-list-item div: <div class="case-list-item <?php echo $activeclass; ?>">

Solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.