Last active
September 21, 2023 20:59
-
-
Save hereswhatidid/fb9152a11bc359fa32772e4d18f01c9a to your computer and use it in GitHub Desktop.
Integrate Yoast SEO Primary Categories with WooCommerce breadcrumbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'woocommerce_breadcrumb_main_term', 'hwid_breadcrumbs_primary_category' ); | |
/** | |
* Override the default term for WooCommerce cookies if Yoast SEO is installed | |
* | |
* @param WP_Term $term | |
* | |
* @return WP_Error|WP_Term|null | |
*/ | |
function hwid_breadcrumbs_primary_category( $term ) { | |
// check to see if the `yoast_get_primary_term_id` function exists | |
if ( function_exists( 'yoast_get_primary_term_id' ) ) { | |
// check if a primary category has been chosen and, if so, return it | |
if ( $primary_id = yoast_get_primary_term_id( 'product_cat' ) ) { | |
return get_term( $primary_id, 'product_cat' ); | |
} | |
} | |
return $term; | |
} |
add_filter( 'woocommerce_product_categories_widget_product_terms_args', 'wp_kama_woocommerce_product_categories_widget_terms_args_filter' );
function wp_kama_woocommerce_product_categories_widget_terms_args_filter( $term ){
// check to see if the yoast_get_primary_term_id
function exists
if ( function_exists( 'yoast_get_primary_term_id' ) ) {
// check if a primary category has been chosen and, if so, return it
if ( $primary_id = yoast_get_primary_term_id( 'product_cat' ) ) {
return get_term( $primary_id, 'product_cat' );
}
}
return $term;
}
This is not working
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
❤