Title: get_parent_page ??
Last modified: August 18, 2016

---

# get_parent_page ??

 *  [jpepper](https://wordpress.org/support/users/jpepper/)
 * (@jpepper)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/)
 * Sorry, but I can’t seem to find this answer. Is there any way to return the name
   of the current page’s parent?
 * I use the following code to control the ‘active’ tabs in my navigation:
 * if (is_page(‘products’)) echo ‘class=”active”‘
 * I also have:
    products/cds products/dvds
 * How can I get WP to return ‘products’ when I am on products/cds for example?
 * Thank!

Viewing 15 replies - 1 through 15 (of 15 total)

 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-195958)
 * You could do something like the following:
 * `if( 0 != $post->post_parent ) {
    $post_name = $wpdb->get_var("SELECT post_name
   from $wpdb->posts WHERE ID = $post->post_parent"); }
 * Untested but should be about what you’d need.
 *  Thread Starter [jpepper](https://wordpress.org/support/users/jpepper/)
 * (@jpepper)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-195972)
 * CF – Thanks for the quick response. I’ll give it a shot.
 *  Thread Starter [jpepper](https://wordpress.org/support/users/jpepper/)
 * (@jpepper)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-195976)
 * CF- I couldn’t get the above to work. $post_name doesn’t return anything. Maybe
   I am using this wrong? I assumed that $post_name would return the parent??
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-195977)
 * Post the whole shebang to [http://pastebin.com](http://pastebin.com) and give
   us a link?
 *  Thread Starter [jpepper](https://wordpress.org/support/users/jpepper/)
 * (@jpepper)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-195984)
 * CF-Is this what you asked?
 * [http://pastebin.com/277596](http://pastebin.com/277596)
 *  Thread Starter [jpepper](https://wordpress.org/support/users/jpepper/)
 * (@jpepper)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-195987)
 * Is get_var a function?
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-195989)
 * Well, the biggest thing is that I was assuming that `$post` was defined at the
   time, like in a Loop. Are you going to be including this code in a template?
 *  Thread Starter [jpepper](https://wordpress.org/support/users/jpepper/)
 * (@jpepper)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-196000)
 * I see. I got it to work in index.php. Thanks.
 * [http://pastebin.com/277606](http://pastebin.com/277606)
 * But I was hoping to use it within header.php because that is where I call my 
   navigation.
 * [http://pastebin.com/277607](http://pastebin.com/277607)
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-196033)
 * Try something like:
    `if( is_page() ) { the_post(); if( 0 != $post->post_parent){
   $post_name = $wpdb->get_var("SELECT post_name from $wpdb->posts WHERE ID = $post-
   >post_parent"); } }
 *  Thread Starter [jpepper](https://wordpress.org/support/users/jpepper/)
 * (@jpepper)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-196037)
 * Perfect. Thanks so much CF!
 * Doesn’t it seem like this should be built in?
 * Maybe someone with more skills than me can craft a simple plugin. If so, would
   it make sense to return an array of all the parents up to the top/root to cover
   the situation of nested pages.
 * Again thanks!
 *  Thread Starter [jpepper](https://wordpress.org/support/users/jpepper/)
 * (@jpepper)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-196040)
 * Well almost perfect. It does successfully get the parent ID when used in the 
   header, but it then messes up the page output generated from index.php. It looks
   like this fails: if (have_posts()) in index.php.
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-196042)
 * This is going to be the death of me. Following that processing, try putting a`
   rewind_posts()` call.
 *  Thread Starter [jpepper](https://wordpress.org/support/users/jpepper/)
 * (@jpepper)
 * [21 years ago](https://wordpress.org/support/topic/get_parent_page/#post-196065)
 * Thanks for sticking with it CF. It worked!
 * Plugin anyone?
 *  [cynics](https://wordpress.org/support/users/cynics/)
 * (@cynics)
 * [20 years, 9 months ago](https://wordpress.org/support/topic/get_parent_page/#post-196538)
 * Hi, is there a way to extend the code above to grab the permalink to the parent
   page?
 *  Thread Starter [jpepper](https://wordpress.org/support/users/jpepper/)
 * (@jpepper)
 * [20 years, 8 months ago](https://wordpress.org/support/topic/get_parent_page/#post-196544)
 * Could you just add the following…
 * `get_permalink($post->post_parent);`

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘get_parent_page ??’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 15 replies
 * 3 participants
 * Last reply from: [jpepper](https://wordpress.org/support/users/jpepper/)
 * Last activity: [20 years, 8 months ago](https://wordpress.org/support/topic/get_parent_page/#post-196544)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
