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!