Archived topic
Display top-most parent (ancestor) page title in addition to current page title
9 replies · Started by Marcus on November 19, 2019
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!
Hi there,
so like a breadcrumb trail based on menu hierarchy?
If so then maybe this stack will help:
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!
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]
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.
[img]https://i.imgur.com/NhxCzfJ.jpg[/img]
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 :)
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();
}
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.
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!!!
Glad we could help! :)