• Resolved heriz

    (@heriz)


    Hello.

    In my sidebar, on my single template, I’m listing all posts under a given category. Code:

    <ul>
    	 <?php
    	 global $post;
    	 $myposts = get_posts('showposts=30&category=175');
    	 foreach($myposts as $post) :
    	 ?>
    		<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    	 <?php endforeach; ?>
    </ul>

    Does anyone have an idea of how to make the current post title highlighted using the style (‘current-post-item’/’current-page-item’ etc) allocated to it?

    I’ve been looking at using something similar to

    <li<?php
    if (is_home())
    {
    echo " class=\"current_page_item\"";
    }
    ?>>

    But I can’t find an ‘is_ ‘ function for the current post title.

    Any help much appreciated!

Viewing 13 replies - 1 through 13 (of 13 total)
  • You can use something like this

    <ul>
    <?php
    
    $IDOutsideLoop = $post->ID;
    global $post;
    
    $myposts = get_posts('showposts=5');
    foreach($myposts as $post) :
    ?>
    
    <li <?php if(is_single() && $IDOutsideLoop == $post->ID) print 'style="font-weight:bold";' ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php endforeach; ?>
    
    </ul>

    Simply adds a statement that will print bold style if the ID outside loop matches the ID inside loop for single post.

    Thread Starter heriz

    (@heriz)

    ahmad – that worked a treat.

    thank you so much!

    ahmad you rock. Here’s the version with a class instead of print

    <ul class="sidebar-ul">
    
    <?php
    
    $IDOutsideLoop = $post->ID;
    global $post;
    
    $myposts = get_posts('showposts=5');
    foreach($myposts as $post) :
    ?>
    
    <li <?php
    if(is_single() && $IDOutsideLoop == $post->ID) 
    
    {
    echo " class=\"current\"";
    }
    ?>>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php endforeach; ?>
    
    </ul>

    of course set your own css

    Hello, the above code is working nicely if you have one list of post.
    But it stop highlighting the current post if you use it to have more than one list.

    Let’s say i need to show
    > Category 1
    >>Post3
    >>Post5

    > Category 2
    >>Post1
    >>Post2
    >>Post4

    How can i tweak the code to make it work?

    I suppose it’s something related to the

    $IDOutsideLoop = $post->ID;
    global $post;

    code, but i don’t know hot to solve it.

    I’ve been trying to do something similar to this, but only view post titles from the current category you are viewing. I have another code snippet which does this, although, it doesn’t have the capability to highlight the currently viewed post.

    <ul>
    	<?php global $post;
    	$categories = get_the_category();
    	foreach ($categories as $category) :
    	?>
    	<h3>More News From This Category</h3>
    	<ul>
    	<?php
    	$posts = get_posts('numberposts=20&amp;category='. $category->term_id);
    	foreach($posts as $post) :
    	?>
    	<li <?php if ( is_category('4') ) { ?> class="current_page_item" <?php } ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php endforeach; ?>
    
    	<?php endforeach; ?>
    </ul>

    I’ve tried merging both together but without success. Anyone know how?

    The code posted here displays posts ONLY from the current category. If only we could merge both together! I don’t know PHP well enough to do this. Anyone?

    <ul>
    
    <?php while(have_posts()) : the_post(); ?>
    
    <?php foreach((get_the_category()) as $category)
    { $my_query = new WP_Query('category_name=' . $category->category_nicename . '&amp;orderby=title&amp;order=asc&amp;showposts=100');} ?> 
    
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile; ?>
    
    <?php break; endwhile; ?>
    
    </ul>

    Brilliant! Thanks for the code. You saved my ass.

    Displaying the post from the current category and highlighting the current post – combines both solutions.

    <ul>
          		<?php foreach((get_the_category()) as $category) { ?>
    				<h3><?php echo $category->category_description . ' '; ?></h3>
                    <?php $catVal = $category->cat_ID; }
    	  		$IDOutsideLoop = $post->ID;
    	  		global $post;
    	  		$myposts = get_posts('category='.$catVal);
    			foreach ($myposts as $post) { ?>
    				<li<?php if($IDOutsideLoop == $post->ID) { echo " class=\"current\""; } ?>>
                    	<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
                <?php }; ?>
           	</ul>

    ^wanna take this a step further and set it so that the categories get displayed all at once together with the lists of posts set under their respective categories (and between the other cats) so that this appears as a list of chapters and subjects? on top of that it might be nice if the posts appeared below their cats only when that cat is active.

    im trying to hack it up myself but am ultra n00b at PHP/Wordpress…

    ok, so it took me a while but here is the code – which i put in my sidebar.php – to list all available categories AND list each category’s posts under their respective categories. the code i’ve pieced together also highlights the current post AND current category by adding a CSS class to each using if statements. coupled with CSS i’ve used this to create an accordion-like nav system of said categories and posts:

    <?php if (is_home(''))
    {
    	wp_list_categories('title_li=&orderby=ID');
    }
    else
    {
    	wp_reset_query();
    	$catVal = get_the_category();
    	$myCat = $catVal[0]->cat_ID;
    
    	$IDOutsideLoop = $post->ID;
    	global $post;
    	?>
    	<?php $categories = get_categories('child_of'.$myCat.'&orderby=ID&order=asc'); /// Parent Category number
    	foreach ($categories as $cat)
    	{
    		$var = $cat->cat_ID ; $lastposts = get_posts('category='.$var.'&orderby=date&order=dsc');
    		wp_list_categories('include='.$cat->cat_ID.'&title_li=');?><?php // echo $cat->cat_name. " (" .count($lastposts).")";?>
    		<ul class="toggle_content<?php if($myCat == $cat->cat_ID) { echo ' current-category'; } ?>">
    		<?php foreach($lastposts as $post)
    		{
    			setup_postdata($post); ?>
    			<li<?php if($IDOutsideLoop == $post->ID) { echo ' class="current-post"'; } ?>><a <?php post_class();?> href="<?=the_permalink()?>"><?=the_title()?><span></span></a></li>
    			<?php
    		} ?>
    		</ul>
    		<?php
    		}
    }?>

    Cornhustlah, you’re a star!
    It comes in handy on a custom build site map page as well.
    Thank you very much,

    Jo.

    Hey guys,

    Currently going through all of this code and am at a complete loss. I’ve used the code that deWINTER posted up so that I can put a “current” class on my posts which I’m using as sidebar navigation.

    When I plunk the code into my sidebar.php it populates with a list of my category + posts that go under it… however the “current” class never gets put in when I’m on that page/post.

    However if I put the code in my header.php, the code works! I threw in a quick “li.current” into my css and it styled the page/post as it should!

    Any thoughts on what might be happening?

    Hi,

    I placed the code in my single.php file in the sidebar div instead of calling the sidebar.php file (I use that for different page templates).
    Would that help?

    Jo.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Highlighting current post title in get_posts list’ is closed to new replies.