0

I'm using get_pages() to create a custom navigation that lists pages with a link, title and thumbnail for each page. How can I add a "current-item" class to the item corresponding to the current page?

I'm using the following code:

<?php
    $our_pages = get_pages( array( 'sort_column' => 'menu_order' ) );
    foreach ($our_pages as $key => $page_item):
?>
    <div class="menu-item">
        <a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>" class="menu-item-clicker"><span><?php echo $page_item->post_title ; ?></span></a>
        <?php echo get_the_post_thumbnail($page_item->ID,'full'); ?>
    </div>
<?php endforeach; ?>

The following does not display the class, but to illustrate what I'm trying to do, here's the conditional I tried, with no success:

<?php
    $our_pages = get_pages( array( 'sort_column' => 'menu_order' ) );
    foreach ($our_pages as $key => $page_item) :
        if($page->ID == $page_item->ID) {
            $class = 'current-item';
        }
?>
    <div class="menu-item <?php echo $class; ?>">
        <a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>" class="menu-item-clicker"><span><?php echo $page_item->post_title ; ?></span></a>
        <?php echo get_the_post_thumbnail($page_item->ID,'full'); ?>
    </div>
<?php endforeach; ?>

Looking forward to your input!

2 Answers 2

1

By $page->ID, I guess you're trying to fetch ID of the current page, right? If so, that's incorrect. Page itself is a post type so you need to get the ID through $post->ID and make sure $post is accessible otherwise use get_the_ID() to return ID of the current post/page.

Final code:

<?php

$our_pages = get_pages( array( 'sort_column' => 'menu_order' ) );
foreach ($our_pages as $key => $page_item) :
    $class = '';
    if($post->ID == $page_item->ID)
        $class = 'current-item';

?>
    <div class="menu-item <?php echo $class; ?>">
        <a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>" class="menu-item-clicker">
            <span><?php echo $page_item->post_title ; ?></span>
        </a>
        <?php echo get_the_post_thumbnail($page_item->ID,'full'); ?>
    </div>
<?php endforeach; ?>
0
1

Just an additional info regarding the answer from @Junaid Ahmed:

You might add this before calling $post->ID:

global $post;
3
  • I guess, this is a comment to my question, so it should go as a comment. And I did mention in my answer "make sure $post is accessible otherwise use get_the_ID()"
    – Junaid
    Jul 3, 2017 at 11:08
  • 1
    You are right. It's just that I don't have the previleg to comment others peoples answers - missing repution right now. I tried. For your example is 99% complete I thought to post the missing 1%. If you add that information to your answer I can delete my answer if you want me to.
    – Luckyfella
    Jul 3, 2017 at 11:14
  • No problem. I just added some to your reputation. You are near your 50 rep mark. Enjoy
    – Junaid
    Jul 3, 2017 at 11:16

Your Answer

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

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