Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using a loop in the sidebar of my site to show all the posts that are in that category. Now I'd like to include a 'current_post' class to the current post that is being displayed to style that item in the list.

I've tried looking for conditional tags or this custom wp_list_post_types function but both haven't worked. Does anybody know if there's a way to do this?

EDIT: Adding Loop from comment below

<?php foreach((get_the_category()) as $category) { 
$postcat= $category->cat_ID; 
$catname =$category->cat_name; } 
$args = array( 'cat' => $postcat ); 
$my_query = new WP_Query(); 
$my_query->query($args); // Equivalent of query_posts()

while($my_query->have_posts()) : $my_query->the_post(); 
$id=get_the_ID();   
$currentClass= ($post->ID == $id) ? "current_post": ""; ?>
<a class="<?php echo   $currentClass; ?>" href="<?php the_permalink();?>">ni</a> 
<?php endwhile; ?>
share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

You should be able to get the queried object id and use that for comparison inside your custom loop..

Before your existing loop code(what you have posted above), but obviously after the opening PHP tag..

global $wp_query;
$current_id = $wp_query->get_queried_object_id();

Then somewhere inside your custom WP loops..

if( $current_id == get_the_ID() ) {
    // This result is the current one
}
else {
    // Not current
}

Hope that helps..

share|improve this answer
    
Yeah, that worked! –  Squrler Oct 21 '11 at 2:06
add comment

If you are using a loop you should be able to do something like this it is untested but should work. Loop for sidebar

<?php if ( have_posts() ) : while( have_posts() ): the_post(); ?>
<?php while( have_posts() ): the_post(); ?>
<?php while( have_posts() ){ 
 $id=get_the_ID();
 $currentClass= ($post->ID == $id) ? "current_post": ""; ?>

Then
<div class="post <?php echo $currentClass; ?>">.....

This isn't tested so I'm sort of just assuming that $post->ID will get the ID of the post outside and get_the_ID() will get the ID of the post inside the loop. If that doesn't work could you post your loop code and I'll do some test

EDIT

There is a few problems with your loop. One is that it's only grabbing the last category. I did a quick google search and found a soultion that seems to work on my test install here

This will only show if they are on a Single post page. Otherwise bad things happen (IE duplicate post showing etc)

    <ul>
<?php
$IDOutsideLoop = $post->ID;
while( have_posts() ) {
    the_post();
if(is_single()){
    foreach( ( get_the_category() ) as $category )
        $my_query = new WP_Query('category_name=' . $category->category_nicename . '&orderby=title&order=asc&showposts=100');
    if( $my_query ) {
        while ( $my_query->have_posts() ) {
            $my_query->the_post(); 
                        $currentClass=( is_single() && $IDOutsideLoop == $post->ID ) ? ' class="current_post"' : '';                    ?>
            <li>
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" <?php echo $currentClass ?>><?php the_title(); ?></a>
            </li>
<?php
        }
    }
}
}
?>
</ul>
share|improve this answer
    
Thank you for replying! Unfortunately, that doesn't seem to work... :S Here's the loop I'm using: <?php foreach((get_the_category()) as $category) { $postcat= $category->cat_ID; $catname =$category->cat_name; } $args = array( 'cat' => $postcat ); $my_query = new WP_Query(); $my_query->query($args); // Equivalent of query_posts() while ($my_query->have_posts()) : $my_query->the_post(); $id=get_the_ID(); $currentClass= ($post->ID == $id) ? "current_post": ""; ?> <a class="<?php echo $currentClass; ?>" href="<?php the_permalink(); ?>">ni</a> <?php endwhile; ?> –  Squrler Apr 12 '11 at 6:32
    
Sorry for the bad formatting of the code, just getting the hang of mini-markdown! :) –  Squrler Apr 12 '11 at 6:40
    
@squrler, it's generally conceded better practice to edit original question with the code. This provides better formatting. I did this for you. However, it must be approved first. –  BandonRandon Apr 12 '11 at 23:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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