WordPress.org

Ready to get started?Download WordPress

Forums

Highlight current post - Advanced Custom Fields (8 posts)

  1. RachelGoldstein71
    Member
    Posted 1 year ago #

    I am using advanced custom fields, and the following code (see below) to display the last 5 posts, but need to change the link color of the current post you are viewing. Would appreciate any help (site is not live or I would post link).

    <div class="leftbar">
    <h3><a href="/blog"><img src="/wp-content/themes/ts/resources/up-arrow.gif" border="0" style="float:left; margin: 3px 5px 0 0;">T&S Blog Overview</a></h3>
    <?php
    $queryObject = new WP_Query( 'post_type=blog&posts_per_page=5' );
    // The Loop!
    if ($queryObject->have_posts()) {
        ?>
        <ul>
        <?php
        while ($queryObject->have_posts()) {
            $queryObject->the_post();
            ?>
    
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php
        }
        ?>
        </ul>
    
        <?php
    }
    ?>
    
    </div><!-- end leftbar -->
  2. Andrew Bartel
    Member
    Posted 1 year ago #

    Use a tool like firebug or chrome developer tools to pinpoint any unique classes or identifiers.

  3. RachelGoldstein71
    Member
    Posted 1 year ago #

    I do use Firebug but there is no class on the current post link that is any different than the rest of the post links.

  4. rencang_enggal
    Member
    Posted 1 year ago #

    declare a class if you didn't. firebug or another tool will show where is it.

  5. RachelGoldstein71
    Member
    Posted 1 year ago #

    I am not sure how to do that -- it's a template page for single posts, and the links of the last 5 posts are dynamically generated. How do you declare a class on the current post? (TIA).

  6. rencang_enggal
    Member
    Posted 1 year ago #

    for example, I assume using your script above.
    <a href="/blog"><img src="">
    you can declare like this:
    <a href="/blog" class="current"><img src="">

    if you can't find on your themes, maybe you can find on your functions file

    CMIIW

  7. RachelGoldstein71
    Member
    Posted 1 year ago #

    The current post would be one of the 5 posts in the loop, not the main blog link. The code above pulls the most recent posts, with the same line of code for each. So that's why I am not sure how to determine which of the 5 links in the loop is the current one for the single post page that I'm on.

  8. RachelGoldstein71
    Member
    Posted 1 year ago #

    I figured it out, and thought I would share here.

    <?php $this_post = $post->ID; ?>
    
    <?php
    $queryObject = new WP_Query( 'post_type=blog&posts_per_page=5' );
    // The Loop!
    if ($queryObject->have_posts()) {
        ?>
        <ul>
        <?php
        while ($queryObject->have_posts()) {
            $queryObject->the_post();
    	$lastFive = $post->ID;
    
            ?>
    	 <?php
    	   if ($this_post == $lastFive)  {
    	 ?>
    	   <li id="current"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    	<?php
    	   }
    	   else  {
    	?>
    	   <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    	   <?php
    	   }
    	   ?>
        <?php
    }
    ?>
        </ul>
    
    <?php
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic