Created
April 30, 2021 13:38
-
-
Save mustardBees/06033e883378be3c43c26a9ea035c5cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Yoast SEO breadcrumbs - link directly to child page when the parent has our | |
* "first child redirect" template. | |
* | |
* @param array $crumbs The complete list of breadcrumbs. | |
* | |
* @return array | |
*/ | |
function kanuka_wpseo_breadcrumbs_first_child_redirect( $crumbs ) { | |
foreach ( $crumbs as $key => $link ) { | |
// Bail if we don't have a post ID. | |
if ( ! isset( $link['id'] ) || empty( $link['id'] ) ) { | |
continue; | |
} | |
$template = get_page_template_slug( $link['id'] ); | |
// Bail if the post doesn't have a template. | |
if ( empty( $template ) ) { | |
continue; | |
} | |
// Bail if this isn't our first child redirect template. | |
if ( 'page-templates/first-child-redirect.php' !== $template ) { | |
continue; | |
} | |
// Grab the first child. | |
$args = array( | |
'numberposts' => 1, | |
'orderby' => 'menu_order', | |
'order' => 'ASC', | |
'post_type' => 'page', | |
'post_status' => 'publish', | |
'post_parent' => $link['id'], | |
); | |
$first_child = get_children( $args ); | |
// Replace breadcrumb link with first child URL. | |
$crumbs[ $key ]['url'] = get_permalink( array_pop( $first_child )->ID ); | |
} | |
return $crumbs; | |
} | |
add_filter( 'wpseo_breadcrumb_links', 'kanuka_wpseo_breadcrumbs_first_child_redirect' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment