2

I have this list set up of the latest 7 posts:

            <ul id="post-list">
            <?php
            global $post;
            $args = array( 'numberposts' => 7 );
            $myposts = get_posts( $args );
            foreach( $myposts as $post ) :  setup_postdata($post); ?>
                <li>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?><br /><span><?php the_author(); ?></span></a>
                </li>
            <?php endforeach; ?>
            </ul>

How can I have a class of 'current' be displayed for the current post?

1

1 Answer 1

2

try this:

<ul id="post-list">
    <?php
        global $post,$wp_query;
    $current_id = $wp_query->get_queried_object_id();
    $args = array( 'numberposts' => 7 );
    $myposts = get_posts( $args );
        foreach( $myposts as $post ) :  setup_postdata($post); ?>
            <li<?php if ($current_id == $post->ID) echo ' class="current"'; ?>>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?><br /><span><?php the_author(); ?></span></a>
                </li>   
    <?php endforeach; ?>
</ul>

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.