Last active
October 25, 2022 17:09
-
-
Save cdils/2d0d0c0d1f4b49b1983f4c3ded05c571 to your computer and use it in GitHub Desktop.
Here's a tutorial for the following snippets showing you how to customize Yoast Breadcrumbs https://carriedils.com/modify-yoast-breadcrumb-text-for-parent-and-child-pages/
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 | |
/** | |
* Filter breadcrumbs on Salary Data pages. | |
* | |
* @param array $link The link array. | |
* @param array $breadcrumb The breadcrumb item array. | |
* | |
* @return str $link The link output. | |
*/ | |
function zg_add_text_to_breadcrumb( $link, $breadcrumb ) { | |
// Do stuff here. | |
return $link; | |
} | |
add_filter( 'wpseo_breadcrumb_single_link', 'zg_add_text_to_breadcrumb', 10, 2 ); |
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 | |
/** | |
* Filter breadcrumbs on Salary Data pages. | |
* | |
* @param array $link The link array. | |
* @param array $breadcrumb The breadcrumb item array. | |
* | |
* @return str $link The link output. | |
*/ | |
function zg_add_text_to_breadcrumb( $link, $breadcrumb ) { | |
// Only run this on salary pages. | |
if ( is_singular( 'salary_data' ) ) { | |
// If we are on the parent page AND targeting the correct breadcrumb in the array. | |
if ( ! has_post_parent() && ( get_the_title() === $breadcrumb['text'] ) ) { | |
// We're on a child page. | |
} else { | |
// Modify parent breadcrumb. | |
global $post; | |
$parent_title = get_the_title( $post->post_parent ); | |
if ( ( $parent_title === $breadcrumb['text'] ) && ( strpos( $link, 'breadcrumb_last' ) === false ) ) { | |
} | |
// Add text to final breadcrumb. | |
if ( strpos( $link, 'breadcrumb_last' ) !== false ) { | |
} | |
} | |
return $link; | |
} | |
} | |
add_filter( 'wpseo_breadcrumb_single_link', 'zg_add_text_to_breadcrumb', 10, 2 ); |
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 | |
/** | |
* Filter breadcrumbs on Salary Data pages. | |
* | |
* Format should be: | |
* Home / Salary Data / [Job] Salary / Salary data for a [Job] in [Location] | |
* | |
* @param array $link The link array. | |
* @param array $breadcrumb The breadcrumb item array. | |
* | |
* @return str $link The link output. | |
*/ | |
function zg_add_text_to_breadcrumb( $link, $breadcrumb ) { | |
// Only run this on salary data pages. | |
if ( is_singular( 'salary_data' ) ) { | |
// If we are on the parent page AND targeting the correct breadcrumb in the array. | |
if ( ! has_post_parent() && ( get_the_title() === $breadcrumb['text'] ) ) { | |
$link_text = get_the_title() . ' Salary'; | |
$link = sprintf( '<span><a href="%1$s">%2$s</a></span>', $breadcrumb['url'], $link_text ); | |
// We're on a child page. | |
} else { | |
// Modify parent breadcrumb. | |
global $post; | |
$parent_title = get_the_title( $post->post_parent ); | |
if ( ( $parent_title === $breadcrumb['text'] ) && ( strpos( $link, 'breadcrumb_last' ) === false ) ) { | |
$link_text = $parent_title . ' Salary'; | |
$link = sprintf( '<span><a href="%1$s">%2$s</a></span>', $breadcrumb['url'], $link_text ); | |
} | |
// Add text to final breadcrumb. | |
if ( strpos( $link, 'breadcrumb_last' ) !== false ) { | |
$title = get_the_title(); | |
// Decide which article to use. | |
$a_or_an = in_array( strtolower( $title[0] ), array( 'a', 'e', 'i', 'o', 'u' ) ) ? 'an ' : 'a '; | |
$link_text = 'Salary data for ' . $a_or_an . get_the_title(); | |
$link = sprintf( '<span class="breadcrumb_last">%1$s</span>', $link_text ); | |
} | |
} | |
return $link; | |
} | |
} | |
add_filter( 'wpseo_breadcrumb_single_link', 'zg_add_text_to_breadcrumb', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment