Forums

Title of page's root ancester (5 posts)

  1. mattgdavies
    Member
    Posted 3 years ago #

    Hi there,

    I wonder if anyone can help a novice like me! I've searched and searched but can't seem to get anything to work.

    Ok - I need to get the page title of the root of a pages ancestor.
    So for example if the site map looked like this:

    • Great Grandparent
      • Grandparent
        • Parent
          • Child

    I'd like it so that wither we were in the Child page,Parent page or GrandParent Page, what would display was the "Great Grandparent" title.

    The reason for this is because the site is to work in sections.

    Any help would be much appreicated

  2. kapiljain.in
    Member
    Posted 3 years ago #

  3. MichaelH
    Volunteer
    Posted 3 years ago #

    This seems to work also:

    <?php
    //get root ancestor for page 234
    $a=get_post_ancestors(234);
    $root=count($a)-1;
    echo "root page id " . $a[$root];
    ?>
  4. kapiljain.in
    Member
    Posted 3 years ago #

    Thanks MichaelH,

    I checked mentioned code, it works fine for the sub level pages but returns nothing for top level pages.

  5. mkormendy
    Member
    Posted 2 years ago #

    In that case .. top level pages would be retrieved from their top level wp_title function. You have to then create an if/else statement that will:

    1. check whether the page is a sub page
    2. if it is a sub-page, find the parent(root page)
    3. if it is a top level page, get the wp_title

    Done.

Topic Closed

This topic has been closed to new replies.

About this Topic