Astra_Breadcrumb_Trail::add_path_parents( string $path )
Get parent posts by path. Currently, this method only supports getting parents of the ‘page’ post type. The goal of this function is to create a clear path back to home given what would normally be a “ghost” directory. If any page matches the given path, it’ll be added.
Description
Parameters
- $path
-
(string) (Required) The path (slug) to search for posts by.
Return
(void)
Source
File: inc/addons/breadcrumbs/class-astra-breadcrumb-trail.php
function add_path_parents( $path ) {
// Trim '/' off $path in case we just got a simple '/' instead of a real path.
$path = trim( $path, '/' );
// If there's no path, return.
if ( empty( $path ) ) {
return;
}
// Get parent post by the path.
$post = get_page_by_path( $path );
if ( ! empty( $post ) ) {
$this->add_post_parents( $post->ID );
}
elseif ( is_null( $post ) ) {
// Separate post names into separate paths by '/'.
$path = trim( $path, '/' );
preg_match_all( "/\/.*?\z/", $path, $matches );
// If matches are found for the path.
if ( isset( $matches ) ) {
// Reverse the array of matches to search for posts in the proper order.
$matches = array_reverse( $matches );
// Loop through each of the path matches.
foreach ( $matches as $match ) {
// If a match is found.
if ( isset( $match[0] ) ) {
// Get the parent post by the given path.
$path = str_replace( $match[0], '', $path );
$post = get_page_by_path( trim( $path, '/' ) );
// If a parent post is found, set the $post_id and break out of the loop.
if ( ! empty( $post ) && 0 < $post->ID ) {
$this->add_post_parents( $post->ID );
break;
}
}
}
}
}
}
Expand full source code Collapse full source code View on Trac
Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |