April 28, 2016
This example will allow for a class to be added to the current child page so it can be custom styled.
Method using the_loop():
$top_menu = new WP_Query($args); global $post; $IDOutsideLoop = $post->ID; if($top_menu -> have_posts()) : while($top_menu -> have_posts()) : $top_menu -> the_post(); <li class="<?php the_title(); ?> <?php if($IDOutsideLoop == $post->ID) { echo "current";}?>"> <a href="<?php the_permalink(); ?>"> <?php the_content(); ?> </a> </li> <?php endwhile; endif; wp_reset_query();
Method using foreach:
<ul> <?php foreach((get_the_category()) as $category) { ?> <h3><?php echo $category->category_description . ' '; ?></h3> <?php $catVal = $category->cat_ID; } $IDOutsideLoop = $post->ID; global $post; $myposts = get_posts('category='.$catVal); foreach ($myposts as $post) { ?> <li<?php if($IDOutsideLoop == $post->ID) { echo " class=\"current\""; } ?>> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php } ?> </ul>
This example, sourced here, drills down to include page ancestors:
$top_menu = new WP_Query($args); global $post; $ancestors = get_post_ancestors($post->ID); $root = count($ancestors) - 1; $parent = $ancestors[$root]; if($top_menu -> have_posts()) : while($top_menu -> have_posts()) : $top_menu -> the_post(); ?> <li class="<?php the_title(); ?> <?php if(is_page() && (is_page($post->ID) || $post->post_parent == $post->ID || in_array($post->ID, $ancestors))) { echo "current";}?>"> <?php the_title(); the_content(); ?> </li> <?php endwhile; endif; wp_reset_query();