Skip to content

Instantly share code, notes, and snippets.

@dospuntocero
Created December 18, 2019 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dospuntocero/9ce70b39a276dad9546325621a95866b to your computer and use it in GitHub Desktop.
Save dospuntocero/9ce70b39a276dad9546325621a95866b to your computer and use it in GitHub Desktop.
child menu with current class using sage roots
public function childMenu() {
global $post;
// Set up the objects needed
$childQuery = new \WP_Query();
$pages = $childQuery->query(array('post_type' => 'page', 'posts_per_page' => '-1'));
if ( is_page() && $post->post_parent ){
$childpages = get_page_children( $post->post_parent, $pages);
}
else{
$childpages = get_page_children( $post->ID, $pages);
}
return $childpages;
}
//we need this to compare with the current page
public function currentUrl() {
return wp_make_link_relative(get_permalink( $post->ID ));
}
<nav class="secondary-nav">
<ul>
@foreach($child_menu as $child)
<li @if(wp_make_link_relative(get_permalink($child->ID)) == $current_url)class="is-active" @endif>
<a href="{!! get_permalink($child->ID) !!}">{{$child->post_title}}</a>
</li>
@endforeach
</ul>
</nav>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment