Reputation: 690
I have a menu created with this code
<?php
$pages = get_pages('child_of= 8&sort_column=post_date&sort_order=asc&parent=8');
foreach($pages as $page) {
?>
<li><a href="<?php $permalink = get_permalink($page->ID);
echo $permalink ; ?>"><?php echo $page->post_title ?></a></li>
<?php } ?>
With this I got the Child Pages of Main about Page. And I need to add active class in this items depending on which page i am(menu created with code above).
Upvotes: 0
Views: 644
Reputation: 2175
You will want to use the following line inside class=''
If(get_the_ID()==$page->ID) echo 'class="active"';
Upvotes: 0
Reputation: 431
You can do that simply by use is_page() to test if the user visit the active page in your menu :
<?php
$pages = get_pages('child_of= 8&sort_column=post_date&sort_order=asc&parent=8');
foreach ( $pages as $page ) {
if ( is_page( $page->ID ) ) {
$active = 'class="active"';
} else {
$active = '';
}
echo '<li '.$active.'><a href="'.get_permalink($page->ID).'">'.$page->post_title.'</a></li>';
}
?>
Upvotes: 2
Reputation: 1102
Add this in your <li>
(or <a>
, as you wish) tag:
<?php if ( get_the_ID() == $page->ID ) echo ' class="active"'; ?>
Upvotes: 0