skip to Main Content

I am using Breadcrumb NavXT and Filter Everything. I am trying to add the product category name to the trail for a custom archive page (not taxonomy archive)

I was able to do something utilizing breadcrumb author’s example and filter author’s function. Here is my code:

add_action('bcn_after_fill', 'my_static_breadcrumb_adder');
function my_static_breadcrumb_adder($breadcrumb_trail)
{
   $output = flrt_selected_filter_terms();

   $w1 = array_keys($output)[0];
   $x1 = $output[$w1]["values"][0];
   $y1 = get_term_by('slug', $x1, 'product_cat');
   $z1 = $y1->name;

   if (isset($z1)) {
      $new_breadcrumb1 = new bcn_breadcrumb($z1, NULL, array('home'), get_site_url() . '/e-shop/' . $w1 . '-' . $x1, NULL, true);
      array_splice($breadcrumb_trail->breadcrumbs, -4, 0, array($new_breadcrumb1));
   }
}

My only problem is that I cannot add the extra trail part to schema.org data (itemListElement->name). Still shows ‘E-SHOP’. It should be the category name.

2

Answers


  1. Chosen as BEST ANSWER

    I reply to this thread of mine with a different issue for the same snippet. After several revisions of the code, it now works as expected, however PHP produces a warning which I would like to resolve.

    The part in question is the following:

    if (is_shop()) {
            $output = flrt_selected_filter_terms();
    
            foreach ($output as $value) {
                $x = $value['values'][0];
                $y = get_term_by('slug', $x, $value['e_name']);
                $z = $y->name;
    
                $new_breadcrumb = new bcn_breadcrumb($z, NULL, array('home'), get_site_url() . '/e-shop/' . $value['slug'] . '-' . $value['values'][0], NULL, true);
                array_splice($breadcrumb_trail->breadcrumbs, -4, 0, array($new_breadcrumb));
            }
        }
    

    It looks as when the bcn object is created, PHP complains:

    PHP Warning:  foreach() argument must be of type array|object, bool given
    

  2. We can achieve the expected output by adding the custom breadcrumb to the breadcrumb trail and also updating the schema.org data.

    add_action( 'bcn_after_fill', 'my_static_breadcrumb_adder' );
    
    function my_static_breadcrumb_adder($breadcrumb_trail) {
        $output = flrt_selected_filter_terms();
    
        if ( empty( $output ) ) {
            return;
        }
    
        $w1 = array_keys( $output )[0];
        $x1 = $output[$w1]['values'][0];
        $y1 = get_term_by( 'slug', $x1, 'product_cat' );
        $z1 = $y1->name;
    
        if ( isset( $z1 ) ) {
            $new_breadcrumb1 = new bcn_breadcrumb( $z1, NULL, array('taxonomy', 'product_cat' ), get_term_link( $y1 ), NULL, true );
            array_splice( $breadcrumb_trail->breadcrumbs, -1, 0, array( $new_breadcrumb1 ) );
    
            // Here we have updated schema.org data.
            $position = count( $breadcrumb_trail->breadcrumbs );
            $breadcrumb_trail->add( new bcn_breadcrumb( $z1, NULL, array( 'taxonomy', 'product_cat' ), get_term_link( $y1 ), NULL, true, $position ) );
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search