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 4 years, 5 months ago by Tom.
-
AuthorPosts
-
November 19, 2019 at 4:37 pm #1067907Marcus
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!
November 20, 2019 at 4:46 am #1068282DavidStaffCustomer SupportHi there,
so like a breadcrumb trail based on menu hierarchy?
If so then maybe this stack will help:November 20, 2019 at 7:25 am #1068608MarcusSuper-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!
November 20, 2019 at 8:11 am #1068652DavidStaffCustomer SupportHeader 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]
November 20, 2019 at 9:41 am #1068767MarcusYou 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]
November 20, 2019 at 5:15 pm #1069397TomLead DeveloperLead DeveloperHi 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 🙂
November 20, 2019 at 7:30 pm #1069504MarcusThank 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(); }
November 21, 2019 at 9:13 am #1071024TomLead DeveloperLead DeveloperOk, 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 try1
.November 21, 2019 at 9:31 am #1071088MarcusThat 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!!!
November 21, 2019 at 4:52 pm #1071607TomLead DeveloperLead DeveloperGlad we could help! 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.