WordPress.org

Support

Support » How-To and Troubleshooting » How to know the id of current page and top parent page

How to know the id of current page and top parent page

  • Hi, I am a newbie, anyone can tell me how to know the id of current page and top parent page of current page. I search the forum but didnt find the result. sorry. can anyone help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • the id of the current page is
    ‘the_ID()’
    unfortunately, i have not yet succeeded in combining the_ID() with other options like “child_of=” etc.

    the id of the parent page … i don’t know. sorry.

    very thanks, your information is very useful. I think, it is possible to get the top parent page by a function. but my php knowledge is poor.

    check this thread:
    http://wordpress.org/support/topic/55192?replies=8

    i screwed up, the ‘the_ID()’ is incorrect and should be called ‘page_ID()’

    what do you want to do with it?

    thanks, in fact, I want to change the blix theme to be used by myself. when I click the page on the top, I hope the children are shown on hte sidebar. it any plugin can do this.

    uff, i can barely manage to find my way in the default theme 🙂
    sorry, children in sidebars dependant on the selected page is currently way beyond my knowledge.
    sorry.

    in fact, I find a way to show the children of the selected page.
    <backticks>
    <?php
    global $post;
    $page_id = $post->ID;
    wp_list_pages(“child_of=$page_id”);
    ?>
    </backticks>

    but I still want to know the top parent page id of current page, so I can highlight the menu.

    I wrote a function to get the top parent page id, would you like help me check it.
    <backticks>
    function BX_top_parent_page()
    {
    global $post;
    $BX_post = $post;
    $topparentpageid = $post->ID;
    while (the_post($topparentpageid)->post_parent) {
    $topparentpageid = the_post($topparentpageid)->post_parent;
    }
    return $topparentpageid;
    }
    </backticks>

    finnally i got it
    <?php

    /**
    * Function BX_archive
    * ——————————————————
    * This function is based on WP’s built-in get_archives()
    * It outputs the following:
    *
    * <h3>Month Year</h3>
    * <ul class=”postspermonth”>
    *

  • Post title (Comment count)
  • * [..]
    *
    */

    function BX_top_parent_page()
    {
    global $post;
    $BX_post = $post;
    $topparentpageid = $BX_post->ID;
    $BX_post_parent_id = $BX_post->post_parent;
    while ($BX_post_parent_id) {
    $BX_post = &get_post($BX_post_parent_id);
    $topparentpageid = $BX_post->ID;
    $BX_post_parent_id = $BX_post->post_parent;
    }

    return $topparentpageid;
    }

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to know the id of current page and top parent page’ is closed to new replies.