WordPress.org

Ready to get started?Download WordPress

Forums

get_parent_page ?? (16 posts)

  1. jpepper
    Member
    Posted 8 years ago #

    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!

  2. ColdForged
    Member
    Posted 8 years ago #

    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.

  3. jpepper
    Member
    Posted 8 years ago #

    CF - Thanks for the quick response. I'll give it a shot.

  4. jpepper
    Member
    Posted 8 years ago #

    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??

  5. ColdForged
    Member
    Posted 8 years ago #

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

  6. jpepper
    Member
    Posted 8 years ago #

    CF-Is this what you asked?

    http://pastebin.com/277596

  7. jpepper
    Member
    Posted 8 years ago #

    Is get_var a function?

  8. ColdForged
    Member
    Posted 8 years ago #

    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?

  9. jpepper
    Member
    Posted 8 years ago #

    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

  10. ColdForged
    Member
    Posted 8 years ago #

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

  11. jpepper
    Member
    Posted 8 years ago #

    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!

  12. jpepper
    Member
    Posted 8 years ago #

    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.

  13. ColdForged
    Member
    Posted 8 years ago #

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

  14. jpepper
    Member
    Posted 8 years ago #

    Thanks for sticking with it CF. It worked!

    Plugin anyone?

  15. cynics
    Member
    Posted 8 years ago #

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

  16. jpepper
    Member
    Posted 8 years ago #

    Could you just add the following...

    get_permalink($post->post_parent);

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.