WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. melzor
    Member
    Posted 5 years 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. Michael Fields
    Themer
    Posted 5 years 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 5 years 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. Michael Fields
    Themer
    Posted 5 years 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 5 years ago #

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

  6. sirhank
    Member
    Posted 5 years 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 5 years ago #

    All sorted now - thanks for the useful code :)

  8. candregg
    Member
    Posted 5 years 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 5 years 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
    }
    ?>
  10. mofotronimus
    Member
    Posted 4 years ago #

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

  11. mofotronimus
    Member
    Posted 4 years ago #

    and the one from stunnaboi works awesome as well - thanks :)

  12. thiebaud
    Member
    Posted 4 years ago #

    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

  13. Frozen Banana
    Member
    Posted 4 years ago #

    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!

  14. bourkela
    Member
    Posted 4 years ago #

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

  15. travellers
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic