Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the following loop on index.php.
The current viewed page would be one of the link in the loop.
How to make sure it is marked as current?

<ul id="questions" class="subpage">
      <?php 

          $index_query = new WP_Query( array( 'post_type' => 'faq', 'orderby' => 'modified', 'posts_per_page' => '-1', 'order' => 'DESC' ) );
          while ( $index_query->have_posts() ) : $index_query->the_post();

        ?>
      <li><a href="<?php the_permalink(); ?>"><?php echo get_field('short_version'); ?> &raquo;</a></li>
      <?php endwhile; ?>
    </ul>

get_field() is a function from Advanced Custom Fields and has the following format:

get_field($field_name, $post_id);

share|improve this question
    
What is get_field()? That's no default WP template tag. –  kaiser Nov 1 '12 at 20:15
1  
@kaiser It's from Advanced Custom Fields –  Zach Nov 1 '12 at 20:26
add comment

2 Answers

up vote 4 down vote accepted

Here's a rewritten version of your code

<ul id="questions" class="subpage">
<?php 
$current_id = get_the_ID();

$index_query = new WP_Query( array( 'post_type' => 'faq', 'orderby' => 'modified', 'posts_per_page' => '-1', 'order' => 'DESC' ) );
while ( $index_query->have_posts() ) : $index_query->the_post();
    if(get_the_ID() == $current_id)
        echo 'this post has the same ID as the current global one';
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php echo get_field('short_version'); ?> &raquo;</a></li>
<?php endwhile; ?>
</ul>

Please note that on the index.php you can have queries of single post types as well as of archives(depending on your theme). If in case of an archive this will match the first post of that archive only. If you want to match for other types of templates, please see the conditional tags

share|improve this answer
    
very strange, but does not work. when I do echo $current_id outside the loop. It is getting 25, but actual ID is 37. Any ideas? –  User789 Nov 1 '12 at 20:39
1  
Got it. This must be within standard loop.<?php while ( have_posts() ) : the_post(); ?> –  User789 Nov 1 '12 at 20:40
add comment

On a single.php page you should have a $post object, which is created by WordPress for those pages. $post->ID is the ID of the 'current' page. Compare that number against the IDs in your loop. If they match 'mark' you post.

Be aware: You may need to save $post to another variable to avoid clobbering it when you run your loop, or use $posts[0] instead of $post.

share|improve this answer
    
Is it $posts[0] because current post always will be first in the array? –  User789 Nov 1 '12 at 20:44
    
Sort of. The $posts array is set on most (all ?) WordPress pages. On index pages it contains all of the posts that will be on the page. On 'single' pages, it only contains one. the_post() defines $post as each post in $posts as you loop through. That is why I think your loop may clobber it. –  s_ha_dum Nov 1 '12 at 20:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.