Skip to content

Instantly share code, notes, and snippets.

@froutsis
Created April 4, 2018 14:05
Show Gist options
  • Save froutsis/123a68df394fc8e41404b125fec523c1 to your computer and use it in GitHub Desktop.
Save froutsis/123a68df394fc8e41404b125fec523c1 to your computer and use it in GitHub Desktop.
Hook wpseo_breadcrumb_links to override category breadcrumbs
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' );
@froutsis
Copy link
Author

froutsis commented Apr 4, 2018

(This uses ACF to map a specific page to a category.)

  1. When the Yoast breadcrumb loads on a singular post then
  • if it finds a category (term) that has a mapped page (using ACF here)
  • it gets replaced by that page.
  1. Also adds the parent of that page on before the page.

The above will not change anything unless a mapped page is found.

Important:

  • Needs to be changed if it is to be used for multiple categories or hierarchical categories.

@jlock6000
Copy link

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:

  • Where do you create the field for mapping the page - is that on the taxonomy?
  • Which ACF field type do you use?

Your help would be much appreciated.

Best regards

@froutsis
Copy link
Author

@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)

@jlock6000
Copy link

@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