ttmt
ttmt

Reputation: 4984

Wordpress get_pages sub nav show current page

I'm using get_pages in Wordpress to create a sub nav on a page.

I know I can use wp_list_pages but I wanted to control the links at different screen sizes in bootstrap.

Using this sort of code is it possible to indicate with a class name the current page in the sub nav.

In wp_list_pages it would be 'current_page_item'

<?php

$page_args = array(

    'child_of' => $post->post_parent,
    'sort_column' => 'ID',
    'sort_order' => 'ASC'

);

$pages = get_pages($page_args);

?>

<div class="container sub-nav">

    <div class="row">

        <div class="col-sm-1"></div>    

<?php
foreach($pages as $page){
?>

            <div class="col-sm-2">

                <a href="'. get_page_link( $page->ID ) .'">

                    <?php echo $page->post_title;?>

                </a>

            </div>
<?php
}
?>

        <div class="col-sm-1"></div>

    </div>

</div>

<?php   
?>
?>

Upvotes: 0

Views: 393

Answers (1)

Kaloyan
Kaloyan

Reputation: 7352

You can simply check if $page's ID is equal to the current page. Something like:

<?php foreach( $pages as $page ):
    $class = ( $page->ID == get_the_id() ) ? 'class="active-or-whatever"' : ''; ?>
    <div class="col-sm-2">
        <a <?php echo $class; ?> href="<?php echo get_permalink( $page->ID ); ?>">
            <?php echo $page->post_title; ?>
        </a>
    </div>
<?php endforeach; ?>

Upvotes: 3

Related Questions