WordPress.org

Support

Support » How-To and Troubleshooting » How do I display post titles from current category and highlight active post

How do I display post titles from current category and highlight active post

  • I’ve been trying to make a sidebar nav that would only display post titles from the current category you are viewing and highlight the current active post (using CSS).

    I have two code snippets and would need to merge them somehow. I don’t know PHP well enough to do this myself.

    Show Posts (all categories), and highlight current one using CSS:

    <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=\"test\"";}?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php endforeach; ?>
    
    </ul>

    Show Posts By Current Category only:

    <ul>
    
    <?php while(have_posts()) : the_post(); ?>
    
    <?php foreach((get_the_category()) as $category)
    { $my_query = new WP_Query('category_name=' . $category->category_nicename . '&orderby=title&order=asc&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>

Viewing 14 replies - 1 through 14 (of 14 total)
  • This worked for me:

    <ul>
    <?php
    $IDOutsideLoop = $post->ID;
    while( have_posts() ) {
    	the_post();
    	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(); ?>
    			<li<?php print ( is_single() && $IDOutsideLoop == $post->ID ) ? ' class="test"' : ''; ?>>
    				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    			</li>
    <?php
    		}
    	}
    }
    ?>
    </ul>

    Not sure what it would do if there were more than one category though?

    Thanks, but I’m getting an error, my code viewer is saying it’s something to do with this line:

    <?php print ( is_single() && $IDOutsideLoop == $post->ID ) ? ' class="test"' : ''; ?>

    thanks!

    This has to do with a problem in the wordpress forums and how it displays ampersands. I post ted the same code in the pasebin, please follow this link:

    http://wordpress.pastebin.com/f77f26294

    It works!!! Thanks so much! kisses!!

    Hi
    I have tried using your code on my website and I have a problem …
    I am using this code in my sidebar to list all the categories as a menu
    <ul id=”leftnav”>
    <?php wp_list_categories(‘orderby=name&title_li=&exclude=1,25’); ?>

    which works great … I put your code into another sidebar so when I click on a link on the menu on the front page it displays the posts and the sidebar menu changes to show the post titles for the current category (using your code)
    The problem I am having is when I first click on the menu bar it is displaying all the posts and the left menu bar shows duplicate post titles … if I then click on 1 of the links in the sidebar the duplicated post titles disappear and it works as it should …

    How can I stop the duplicating of the post titles please?

    Thanks for your help
    PS hope you can understand the problem …

    All sorted now – thanks for the useful code 🙂

    I used this code to do show all the posts in a category in the sidebar, and ran into the same problem that sirhank did: it repeated the names of posts over and over.

    Can I please get the fix that allows it to show each post in that category only once? Also, if there’s a hack that both 1) shows each post name only once and 2) excludes the current post form the list, I’d love to know that one.

    I ain’t no PHP wiz, but I got something to work that won’t duplicate the titles:

    <?php
    foreach( ( get_the_category() ) as $category ) {
    $the_query = new WP_Query('category_name=' . $category->category_nicename . '&showposts=5');
    while ($the_query->have_posts()) : $the_query->the_post();
    ?>
    			<li>
    				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    			</li>
    <?php endwhile; ?>
    <?php
    }
    ?>

    just a general thank you to you all for your help! the code pasted from mfields worked for me 😀

    and the one from stunnaboi works awesome as well – thanks 🙂

    Yes. It works.
    The problem I have: if I show a page (not post), I get a list of posts whith no sense.
    How can I have display post titles from current category and display nothing when there is no post.
    Thank you !
    Marc

    Hi,
    I am placing stunnaboi’s code in the PHP code widget for my sidebar and nothing happened. Am I missing something? Is there a special place I need to put this for it to be implemented? Sorry if this is a very basic question – new to php!
    thanks!

    @mfields, Thanks for the post! This was helpful.

    @frozenbanana
    me too – don’t know where to put this code to make it work, wherever I put it nothing changes on the sidebar

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘How do I display post titles from current category and highlight active post’ is closed to new replies.
Skip to toolbar