The WooCommerce documentation has mentioned everything with a clear description. But we have added a code snippet mentioned in this post that will help you customize WooCommerce breadcrumb at a specific index in the array BreadCrumbs.
I know sometimes we or our client needs something specific that isn’t provided by WordPress or WooCommerce by default. WordPress allows customizing everything single thing using hooks or filters, which is one of the biggest advantages of using WordPress.
Please copy and paste this code block into your
functions.php file and update the code as per your requirement, if you don’t know whats going on then continue reading to understand the code.
If you aren’t a web developer and don’t understand the code above then let’s understand it a little bit. The above hook will get your breadcrumbs in an Array structure. You can learn more about an Array structure online, but in a nutshell, it is a list of similar kinds of objects, in our case, it will be BreadCrumbs.
BreadCrumbs objects are itself an Array containing 2 values.
- BreadCrumb title that you see on the web page
- BreadCrumb URL that user gets redirect to if they click on the BreadCrumb
In the code above we have used
array_splice() function to add a new BreadCrumb to the
$crumbs array and return the new
$crumbs afterward. In the
array_splice() we have passed an array containing title and URL for the new Breadcrumb that we want to add at the second index (we have passed 1 as the second parameter because in Array structure the list index starts from 0 instead of 1)
You can add some condition before the
array_splice() function if you want to ignore adding/modifying BreadCrumbs for specific pages
This way you can customize the
$crumbs to add a new breadcrumb or remove an existing breadcrumb or edit an existing breadcrumb using different PHP functions easily. If you need any help then please let us know in the comment section.