Information:
1. Using Wordpress and WooCommerce, alongside the Genesis theme (+ Genesis Connect for WooCommerce plugin)
2. Shop archive page is /Shop
3. Breadcrumbs are activated for all pages (e.g. Shop, Category/Archive and Single Product pages)
4. "Products" (permalink: /Products
) is a separate Wordpress page (not a category)
Trying to achieve the following Breadcrumb Conditions:
1. Add "Products" crumb (name: Products
, link: /products
) right after "Home" crumb (e.g. Home > Products > Category > Single Product)
2. Do not show "Products" crumb if inside Shop page (e.g. Home > Shop)
I have achieved a functioning result using the following code:
/*
* Add "Products" to breadcrumb (ex. Home > Products > etc), but do not show "Products" crumb in Shop page (ex. Home > Shop)...
*/
add_filter( 'woocommerce_breadcrumb_defaults', 'custom_woocommerce_breadcrumbs' );
function custom_woocommerce_breadcrumbs() {
$home = get_home_url();
$home_name = (is_shop()) ? 'Home' : 'Products';
$home_encoded_link = (is_shop()) ? '' : '<a href="' . $home . '">Home</a> » ';
return array(
'delimiter' => ' » ',
'wrap_before' => '<nav class="woocommerce-breadcrumb" itemprop="breadcrumb">' . $home_encoded_link,
'wrap_after' => '</nav>',
'before' => '',
'after' => '',
'home' => _x( $home_name, 'breadcrumb', 'woocommerce' ),
);
}
add_filter( 'woocommerce_breadcrumb_home_url', 'custom_breadcrumb_home_url' );
function custom_breadcrumb_home_url() {
$home = get_home_url();
return $home . '/products/';
}
The question is: Is there a better way to do this? My solution feels a little "hacky" to me and I was wondering if I'm overlooking an obvious & smarter way to achieve the results I want.