Support » Fixing WordPress » get_parent_page ??

  • jpepper

    (@jpepper)


    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

    (@coldforged)

    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.

    jpepper

    (@jpepper)

    CF – Thanks for the quick response. I’ll give it a shot.

    jpepper

    (@jpepper)

    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

    (@coldforged)

    Post the whole shebang to http://pastebin.com and give us a link?

    jpepper

    (@jpepper)

    CF-Is this what you asked?

    http://pastebin.com/277596

    jpepper

    (@jpepper)

    Is get_var a function?

    ColdForged

    (@coldforged)

    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?

    jpepper

    (@jpepper)

    I see. I got it to work in index.php. Thanks.

    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

    ColdForged

    (@coldforged)

    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");
    }
    }

    jpepper

    (@jpepper)

    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!

    jpepper

    (@jpepper)

    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

    (@coldforged)

    This is going to be the death of me. Following that processing, try putting a rewind_posts() call.

    jpepper

    (@jpepper)

    Thanks for sticking with it CF. It worked!

    Plugin anyone?

    Hi, is there a way to extend the code above to grab the permalink to the parent page?

    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.