Created
April 4, 2018 14:05
-
-
Save froutsis/123a68df394fc8e41404b125fec523c1 to your computer and use it in GitHub Desktop.
Hook wpseo_breadcrumb_links to override category breadcrumbs
This file contains hidden or 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
function custom_override_yoast_breadcrumb_trail( $links ) { | |
global $post; | |
if ( is_singular() ) { | |
$bit_term_key = 0; | |
$bit_parent_page = 0; | |
foreach ( $links as $key => $link ) { | |
// Is there a term / category within a breadcrumn in a singular post | |
if ( isset( $link['term'] ) ) { | |
// Make sure ACF is present | |
if( function_exists('get_field') ){ | |
// Get the corresponing page to this term/category | |
$bit_connected_page = get_field( 'bit_connected_page', $link['term'] ); | |
if( ! empty( $bit_connected_page ) ){ | |
// Replace the term with this page and let Yoast do tht rest | |
$links[$key] = array( 'id' => $bit_connected_page->ID ); | |
// Keep the key as a reference to insert custom /blog page in front of. | |
$bit_term_key = $key; | |
// Keep the parent id to locate custom /blog page in front of. | |
$bit_parent_page = $bit_connected_page->post_parent ; | |
} | |
} | |
} | |
} | |
// Also insert the custom /blog page in front of the term | |
if($bit_term_key > 0 && $bit_parent_page > 0){ | |
return array_merge( | |
array_slice( $links, 0, $bit_term_key ), | |
array( array( 'id' => $bit_parent_page )), | |
array_slice( $links, $bit_term_key ) | |
); | |
} | |
} | |
return $links; | |
} | |
add_filter( 'wpseo_breadcrumb_links', 'custom_override_yoast_breadcrumb_trail' ); |
@jlock6000 this is quite old but yes.
The ACF field is on the Taxonomy and it is a Post Object (return Object and not ID)
@froutsis Thanks a lot for getting back to me so quickly. Much appreciated!
I still have some problems getting it to work. I see that you do freelance work. Would you be interested in taking on this task and fixing it on my project? If so, please get in touch with me at [email protected]
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Froutis
I stumbled upon your snippet here and tried to make use of it, but can't get it to work. Could you be so kind to elaborate:
Your help would be much appreciated.
Best regards