• Hello!

    For my particular site and for several reasons, I can’t use wp_list_pages for a nav menu in part of my theme. So I use the following code instead:

    <ul>
    <li>
    <?php $parent_link = get_permalink($post->post_parent); ?>
    <a href="<?php echo $parent_link; ?>"><?php $parent_title = get_the_title($post->post_parent); echo $parent_title; ?></a></li>
    
    <?php 
    
    $mypages = get_pages('child_of='.$post->post_parent.'&sort_column=menu_order&hierarchical=0');
    
    	if($mypages)
    	{
    		foreach($mypages as $page)
    		{
    	?>
    	<li>
    	<a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a>  </li>
    
    	<?php
    		}
    	}
    ?>
    </ul>

    How do I add a “current” class for the page that I’m currently on? I figure I could do it using some jquery or something (not sure, but I imagine I could), but I’d like to do it in the php if at all possible. Can someone give me a hand? Maybe something I need to add in my functions.php? Or do I keep it in this template? I imagine that it’d be through a simple IF statement right inside the href where I echo the page link, but I’m not quite sure how to do it.

    Thanks!

Viewing 1 replies (of 1 total)
  • Thread Starter Richard Archambault

    (@richardmtl)

    Got it!

    <?php
    $current_id = get_the_ID();
    $mypages = get_pages('child_of='.$post->post_parent.'&sort_column=menu_order&hierarchical=0');
    
    	if($mypages)
    	{
    		foreach($mypages as $page)
    		{
    		$link_page_id = ($page->ID)
    	?>
    
    <li>
    	<a <?php if ($current_id == $link_page_id) : ?> class="current" <?php else : ?><?php endif; ?>class="current" <?php else : ?><?php endif; ?> href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a>
    
    </li>
    	<?php
    		}
    	}
    ?>
Viewing 1 replies (of 1 total)

The topic ‘Adding "current" class if using get_pages’ is closed to new replies.