WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Highlighting current post

[Resolved] Highlighting current post

  • I have a page that currently lists posts on a table row (like a nav). I would want to highlight the current row based on the what page the user is on.

    —–
    article 1
    —–
    article 2
    —–
    article 3 highlighted (user is on article 3 page)
    —–

    Been trying to look for a solution everywhere but have yet to come up with one.

    Any help would be appreciated.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Fisrt we should clarify are you talking about posts or Pages?

    Posts

    /bump

    Don’t bump after an hour or two!
    Patience is highly appreciated by the volunteer helpers.
    Probably nobody came around that has an answer for your question. For sure, I don’t.

    Perhaps you could elaborate on the details? Sounds like you are trying to do some custom work here, and using the post ID might be the way to go. It’s hard to say without knowing the context though.

    Sorry about bumping to quick.

    To elaborate, here is the code Im using to output the posts.

    <? $pageid="post-<?php the_ID(); ?>"; ?>
    <ul>
    <?php $lastposts = get_posts('numberposts=20'); foreach($lastposts as $post) : setup_postdata($post); ?>
    <li>
    <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a>
    </li>
    <?php endforeach; ?>
    </ul>

    All I basically want is to have a css class applied to the list. Ive tried the following below.

    <? $pageid="post-<?php the_ID(); ?>"; ?>
    <ul>
    <?php $lastposts = get_posts('numberposts=20'); foreach($lastposts as $post) :
    setup_postdata($post); ?>
    <li <? if("post-<?php the_ID(); ?>"==$pageid){echo 'id="current"';}?>><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>

    The problem with this is that all outputed links are highlighted.

    Please mark this as solved. I have found the solution.

    I just added a css property to match the post id


    <style type="text/css">
    #post-<?php the_ID(); ?> {
    background-color: #FFFFFF;
    }
    </style>
    <li class="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a>
    </li>

    You should mark it solved 🙂
    (up where the title is)

    sory, i dont get this -if i try this solution, all links are highlighted?

    I used the code of machinetype (thanks), but instead of highlight like this:

    post 1
    post 2
    “post 3 highlight”
    post 4

    Put some other title down the other post title, like this:

    post 1
    post 2
    post 3
    post 4
    “post 3 highlight”

    Is that what it spouse to do, or I put it wrong?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Resolved] Highlighting current post’ is closed to new replies.