Detecting child page and showing parent title and subnav
-
I’ve done a custom theme with top-level pages, each with their own child pages. I added a function is_child to my functions file and then added the following code to my page template – the goal is to detect if the page is a child page and, if so, display the parent page title and the subnav for that section:
<?php if(is_child(32)) { $parent_title = get_the_title($post->post_parent); echo "<h6>$parent_title</h6>"; } ?> <div id="sub-navigation"> <?php if (is_child(32)){ wp_nav_menu( array('menu' => 'Education Subnav' )); } ?> </div>
This works fine, but I’m not very good at PHP and Im thinking there must be a more elegant solution. I couldn’t get this to work without putting the page ID in the is child function. I’d like to know if there’s a way to just have this code once, where it would detect the parent page and display the title and subnav without my having to repeat it with the page ID for each page. Thx for any help.
- The topic ‘Detecting child page and showing parent title and subnav’ is closed to new replies.