0

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);

2
  • What is get_field()? That's no default WP template tag.
    – kaiser
    Commented Nov 1, 2012 at 20:15
  • 1
    @kaiser It's from Advanced Custom Fields
    – Zach
    Commented Nov 1, 2012 at 20:26

2 Answers 2

4

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

2
  • 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?
    – Iladarsda
    Commented Nov 1, 2012 at 20:39
  • 1
    Got it. This must be within standard loop.<?php while ( have_posts() ) : the_post(); ?>
    – Iladarsda
    Commented Nov 1, 2012 at 20:40
1

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.

2
  • Is it $posts[0] because current post always will be first in the array?
    – Iladarsda
    Commented Nov 1, 2012 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
    Commented Nov 1, 2012 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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