[Resolved] Display top-most parent (ancestor) page title in addition to current page title

Home Forums Support Display top-most parent (ancestor) page title in addition to current page title

  • This topic has 9 replies, 3 voices, and was last updated 10 months ago by Tom.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1067907
    Marcus

    I’m trying to figure out a way (a hook perhaps?) to display the top-most PARENT (ancestor) Page’s Title in addition to the current page title. That way whenever the user is browsing page deeper in the site, like second or third-level pages, they still have a sense of what main section of the site they are.

    Top-most PARENT Page Name · Page Title One”

    -OR-

    Top-most PARENT Page Name · Sub-sub-page Title Three

    Ideally I would like it to be referencing the hierarchy and order of my main/global WP menu.

    Any tips or direction would be most appreciated!

    #1068282
    David
    Staff
    Customer Support

    Hi there,

    so like a breadcrumb trail based on menu hierarchy?
    If so then maybe this stack will help:

    https://stackoverflow.com/a/52040437

    #1068608
    Marcus

    Super-helpful link David… definitely gets me closer!

    I was able to get that code to render, but when on third-level pages, it’s returning two names, instead of the top-most parent. I guess I could always hide the second name with a bit of CSS.

    Right now I have a GP header element with:

    <h1>{{post_title}}</h1>

    …and I would love to take the code you referenced and somehow put that parent page name below.

    <h2>{{***PARENT PAGE NAME HERE***}}</h2>

    That way the user knows immediately what page they are on and within what section of the site. I hope that makes sense.

    Thanks again for the excellent help this far!

    #1068652
    David
    Staff
    Customer Support

    Header Elements don’t support PHP – but they do support Shortcodes, so you could wrap the ouput function within a shortcode e.g

    <?php
    
    add_shortcode( 'custom_breadcrumb', 'db_custom_breadcrumb' );
    
    function db_custom_breadcrumb() {
        ob_start();
        
        $parentitems = my_menu_parent( 'header-menu' );
        foreach ( $parentitems as $parentitem ) {
            echo $parentitem."<br>";
        }
    
        return ob_get_clean();
    
    }
    ?>

    Then call it using [custom_breadcrumb]

    #1068767
    Marcus

    You rule David!

    So close to having this sorted… The shortcode worked a treat. I just can’t figure out how to get the foreach loop to return ONLY the top-most parent / ancestor.

    #1069397
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    In your my_menu_parent function, try replacing this:

    return $breadcrumbs;

    With this:

    return $breadcrumbs[0];

    Let me know if that works or not 🙂

    #1069504
    Marcus

    Thank you for jumping in on this Tom. Adding the “[0]” to the return made the text disappear instead of returning the first value of the array. Hmmm…

    Here’s the function and shortcode block within my functions.php file if that helps:

    function my_menu_parent($theme_location) {
        $locations = get_nav_menu_locations();
        if ( isset( $locations[ $theme_location ] ) ) {
            $menu = wp_get_nav_menu_object( $locations[ $theme_location ] );
            $menu_items = wp_get_nav_menu_items($menu->term_id);
            _wp_menu_item_classes_by_context( $menu_items );
            $breadcrumbs = array();
    		
            foreach ( $menu_items as $menu_item ) {         
                if ($menu_item->current_item_ancestor) {
                    $breadcrumbs[] = $menu_item->title;
                }
            }
            return $breadcrumbs;
         }
    }
    
    add_shortcode( 'custom_breadcrumb', 'db_custom_breadcrumb' );
    
    function db_custom_breadcrumb() {
        ob_start();
        $parentitems = my_menu_parent( 'primary' );
        foreach ( $parentitems as $parentitem ) {
            echo $parentitem."<br>";
        }
        return ob_get_clean();
    }
    #1071024
    Tom
    Lead Developer
    Lead Developer

    Ok, what if we replace this:

    foreach ( $parentitems as $parentitem ) {
        echo $parentitem."<br>";
    }

    With this:

    echo $parentitems[0];

    We should be able to target that first item without looping through all of them. If it’s not 0, maybe try 1.

    #1071088
    Marcus

    That did it!

    Don’t know why I didn’t think to just eliminate the loop altogether. 😉

    Thank you so much Tom and David. You guys ROCK!!!

    #1071607
    Tom
    Lead Developer
    Lead Developer
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.