WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Highlight item in list of posts based on current post (6 posts)

  1. katart17
    Member
    Posted 3 years ago #

    I want to have a list of all post titles that link to the single post, but that list of post titles will be on the single post page. Kind of like a Menu. I've done this by Querying post Titles and Querying Post content on the same page. The problem I am having is that the menu (list of post titles) does not reflect the current Post. This is not helpful when I want to use CSS to highlight the Post Title.

    I figure this is easy with Pages, but I am using a Custom Post Type for this. There will be around 15-20 Posts and I don't want to deal with using a Nav Menu.

    Any ideas on how to do this?

  2. katart17
    Member
    Posted 3 years ago #

    I guess what I am trying to do is this:

    if the ID is == the Post ID then set that menu item class to current.

    I have added this to my menu:

    <?php while ( have_posts() ) : the_post(); ?>
                    <li class="<?php if( the_id() == $post_id ) { ?>current<?php } else { ?> <?php } ?>"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
                <?php endwhile; ?>

    All this does is create a class with the Post ID and current. Here is an example:

    <li class="34current"></li>
    <li class="35current"></li>
    <li class="36current"></li>

    What I would like to have is just a class of current and on only one list item.

    Any ideas?

  3. katart17
    Member
    Posted 3 years ago #

    I'm getting closer. Now I have called the correct Post ID for each menu item using

    <li class="<?php echo $post->ID; ?>">

    but now all I need is to say if the Post ID for the current page is the same as the Post ID for the menu item, echo "current" for that item only.

  4. katart17
    Member
    Posted 3 years ago #

    WOW! I figured it out.

    Here is the code I came up with for anyone who wants it or can optimize it.

    <?php $myposts = get_posts('post_type=diseases&numberposts=999&orderby=title&order=ASC'); ?>
                	<?php $postid = get_the_id();
    					foreach( $myposts as $post) :
    				?>
                    	<li class="<?php if( $post->ID == $postid ) { echo 'current'; } ?>"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
                    <?php endforeach; ?>

    Sorry for writing down my thoughts, but I think it helped.

  5. isocreative
    Member
    Posted 3 years ago #

    Thank you katart17!!

    I've been trying tinker with the built in function for pages to use on posts to no avail. I'm surprised this isn't requested more often, or built in.

    For my own purposes, I needed it to work with a list by category, which worked just as well with your script.


    <?php $myposts = get_posts('category=4&numberposts=999&orderby=title&order=ASC'); ?>
    <?php $postid = get_the_id();
    foreach( $myposts as $post):
    ?>
    <li class="<?php if( $post->ID == $postid ) { echo 'current'; } ?>">" title="<?php the_title(); ?>"><?php the_title(); ?>
    <?php endforeach; ?>

  6. prowebdesign.ro
    Member
    Posted 3 years ago #

    Hi katart17. Thanx for the function - it was just what I was looking for.
    Just one small thing to make it better. Your code generates empty classes, if the post is not current. If you don't need an explicit class on the li tag, you can have class only on current posts like this:

    <?php $myposts = get_posts('post_type=diseases&numberposts=999&orderby=title&order=ASC'); ?>
                	<?php $postid = get_the_id();
    					foreach( $myposts as $post) :
    				?>
                    	<li<?php if( $post->ID == $postid ) { echo ' class="current"'; } ?>><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
                    <?php endforeach; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic