WordPress: breadcrumbs for custom taxonomies with Yoast’s SEO

WordPress SEO is a very good plugin developed by Yoast.
It has a ton of interesting features including a nice and easy to use ‘Breadcrumb‘.

It works like a charm until you have to integrate a custom taxonomy in a page:
for example I had to insert a Portfolio section managed by BWS Porfolio Pugin in normal page.

In my case (this site, portfolio section) i have a custom taxonomy for the portfolio in witch every post is a project I’ve developed.
Then the page ‘Portfolio’ retrieves the last entries and prints it like a normal blog homepage.
This is managed by BWS Portfolio plugin.

The issue is that for WordPress the taxonomy is not child of the page; this cause the single article to be son of post 0 (the homepage).
This is not logically correct for me. So I want to insert an entry between the article and the homepage.

This is the code to do it:

and have to be placed in the template file used by the portfolio page and by the single portfolio item. If you plan to put it in functions.php you’ll have to insert some conditional to add the desired ancestor only when needed.

The trick it’s quite simple: we have to cheat on $post->post_parent so that the Yoast’s code will assume the current article is child of the page ‘Portfolio’.
This is done by adding a filter called ‘wp_seo_get_bc_ancestors‘.

I know this is a workaround, maybe someday Yoast will implement something better to solve this little issue. For now this piece of code make WordPress SEO Breadcrumbs and BWS Portfolio work together.

  • Emanuele, sorry if this is off-topic, but I’ve been searching everywhere. Do you know if it’s possible to place — either in functions.php or the post template code that will generate the breadcrumb path according to a custom taxonomy (e.g., “products”) IF the post is assigned to a term in that taxonomy, ELSE the breadcrumb path will be according to the “category” taxonomy by default? (I realize in the WordPress SEO settings, one can choose one or the other.) Thanks for the assist!

    • Well I think this should be possibile by adding some checks in add_portfolio_taxonomy() function.

      Something like

      global $post;
      $taxes = wp_get_post_terms($post->ID, $taxonomy);
      if(in_array($id_of_the_term_i_want_to_check, $taxes)){
      $ancestors[] = $id_of_the_term_i_want_to_check;

      if you only want to check if the post has any custom tax change the if to:
      $ancestors[] = $taxes[0]; // or something more complicate to choose the right tax.

      You can also cycle on $ancestors to remove the entry you don’t want to have.
      It should be the last one so array_pop() will help you.

      Can this be a solution to your problem?