Here is a section of code that I have in a template file — to display sub-navigation if the page has child-pages:
<?php // display sub-nav if page has children ?>
<?php $children = get_pages(array('child_of' => $post->ID)); ?>
<?php if (count($children)) : ?>
<ul class="nav nav-tabs">
<?php foreach ($children as $val) : ?>
<li role="presentation">
<a href="<?php echo get_permalink($val->ID); ?>"><?php echo $val->post_title; ?></a>
</li>
<?php endforeach; ?>
</ul>
The code works fine when in the template file page.php
, but if I put it all into a new file — nav.php
and then include it with <?php get_template_part( 'include', 'nav' ); ?>
then it stops working.
How can I set it so that the $post
variable still works? Do I need to do something with global variables?