WordPress.org

Ready to get started?Download WordPress

Forums

How to know the id of current page and top parent page (8 posts)

  1. freebies
    Member
    Posted 8 years ago #

    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.

  2. mores
    Member
    Posted 8 years ago #

    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.

  3. freebies
    Member
    Posted 8 years ago #

    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.

  4. mores
    Member
    Posted 8 years ago #

    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?

  5. freebies
    Member
    Posted 8 years ago #

    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.

  6. mores
    Member
    Posted 8 years ago #

    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.

  7. freebies
    Member
    Posted 8 years ago #

    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>

  8. freebies
    Member
    Posted 8 years ago #

    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">
    *

  9. Post title (Comment count)
  10. * [..]
    *
    */

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags