Skip to content

Instantly share code, notes, and snippets.

@doubleedesign
Created December 4, 2018 00:52
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save doubleedesign/7224a5e990b8506ddb8ec66de8348b2b to your computer and use it in GitHub Desktop.
Save doubleedesign/7224a5e990b8506ddb8ec66de8348b2b to your computer and use it in GitHub Desktop.
Mark up Yoast breadcrumbs as an unordered list
<?php
/**
* Filter the output of Yoast breadcrumbs so each item is an <li> with schema markup
* @param $link_output
* @param $link
*
* @return string
*/
function doublee_filter_yoast_breadcrumb_items( $link_output, $link ) {
$new_link_output = '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb">';
$new_link_output .= '<a href="' . $link['url'] . '" itemprop="url">' . $link['text'] . '</a>';
$new_link_output .= '</li>';
return $new_link_output;
}
add_filter( 'wpseo_breadcrumb_single_link', 'doublee_filter_yoast_breadcrumb_items', 10, 2 );
/**
* Filter the output of Yoast breadcrumbs to remove <span> tags added by the plugin
* @param $output
*
* @return mixed
*/
function doublee_filter_yoast_breadcrumb_output( $output ){
$from = '<span>';
$to = '</span>';
$output = str_replace( $from, $to, $output );
return $output;
}
add_filter( 'wpseo_breadcrumb_output', 'doublee_filter_yoast_breadcrumb_output' );
/**
* Shortcut function to output Yoast breadcrumbs
* wrapped in the appropriate markup
*/
function doublee_breadcrumbs() {
if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<ul>', '</ul>');
}
}
@enachke
Copy link

enachke commented Jan 24, 2022

Hi!
Any ideea how to implement
meta itemprop="position" content="' . $count . '" on $new_link_output and get the $count value?
Thanks!

@gusmt66
Copy link

gusmt66 commented May 12, 2022

Thank you very much for this! I just copied 2 of these functions and they worked like a charm. Thank you!

@doubleedesign
Copy link
Author

doubleedesign commented May 12, 2022

Thank you very much for this! I just copied 2 of these functions and they worked like a charm. Thank you!

Thanks for your comment @gusmt66, I'm so glad to hear this helped you!

@GinJus
Copy link

GinJus commented Jun 2, 2022

Add this so the HTML validates:

function filter_wpseo_breadcrumb_separator($separator) { return '<li>'.$separator.'</li>'; }; add_filter('wpseo_breadcrumb_separator', 'filter_wpseo_breadcrumb_separator', 10, 1);

@mstudioIL
Copy link

Thanks for code, I can't see the <ul></ul> is show only the <li></li>
I using shortcode or the Elementor widget
how can I control the wrapping

@doubleedesign
Copy link
Author

Thanks for code, I can't see the <ul></ul> is show only the <li></li> I using shortcode or the Elementor widget how can I control the wrapping

Hi @mstudioIL, I don't generally work with Elementor so I don't know the answer to this one. Sorry I can't be more help!

@mstudioIL
Copy link

Thanks, the last code
function doublee_breadcrumbs() { if ( function_exists('yoast_breadcrumb') ) { yoast_breadcrumb('<ul>', '</ul>'); } }
need to be in page and not in
It didn't worked in functions.php, the rest of the code is working.
Is there way to change this with functions.php?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment