Forums

How do I display post titles from current category and highlight active post (9 posts)

  1. melzor
    Member
    Posted 7 months ago #

    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>
  2. mfields
    Member
    Posted 7 months ago #

    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?

  3. melzor
    Member
    Posted 7 months ago #

    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!

  4. mfields
    Member
    Posted 6 months ago #

    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

  5. melzor
    Member
    Posted 6 months ago #

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

  6. sirhank
    Member
    Posted 6 months ago #

    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 ...

  7. sirhank
    Member
    Posted 6 months ago #

    All sorted now - thanks for the useful code :)

  8. candregg
    Member
    Posted 4 months ago #

    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.

  9. stunnaboi
    Member
    Posted 4 months ago #

    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
    }
    ?>

Reply

You must log in to post.

About this Topic