WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Highlighting current post (11 posts)

  1. machinetype
    Member
    Posted 7 years ago #

    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.

  2. moshu
    Member
    Posted 7 years ago #

    Fisrt we should clarify are you talking about posts or Pages?

  3. machinetype
    Member
    Posted 7 years ago #

    Posts

  4. machinetype
    Member
    Posted 7 years ago #

    /bump

  5. moshu
    Member
    Posted 7 years ago #

    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.

  6. Xander
    Member
    Posted 7 years ago #

    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.

  7. machinetype
    Member
    Posted 7 years ago #

    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.

  8. machinetype
    Member
    Posted 7 years ago #

    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>

  9. moshu
    Member
    Posted 7 years ago #

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

  10. keasone
    Member
    Posted 6 years ago #

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

  11. kaeloz
    Member
    Posted 6 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.