iaffpro_custom_attribute_tag_category_taxonomy

Filter the taxonomy name for %category% custom attribute tag to extend it to categories of other post types. By default %category% fetches the category of the WordPress post post type.

The taxonomy name for the WordPress post‘s category is category and that of WooCommerce product category is product_cat.

Taxonomy Name Of WordPress Post Category Is Category
Taxonomy name of WordPress Post category is category.
Taxonomy Name Of WooCommerce Product Category Is Product_cat
Taxonomy name of WooCommerce Product Category is product_cat.
/**
 * Filter $category_taxonomy_name to extend %category% custom 
 * attribute tag to other post types. 
 * 
 * For example, if you have a custom post type named 'library' where 
 * the category taxnomy name is 'genre',
 you can return the 
 * category taxonomy name so that the name of the genre can be retrieved. 
 * 
 * Refer 3rd-party/woocommerce.php for example code.
 * 
 * @since 3.0
 * 
 * @param $category_taxonomy_name (string) Name of the taxonomy.
 * @param $post_type (string) will have the post type of the parent post where 
 * the image is used.
 */
$category_taxonomy_name = apply_filters( 'iaffpro_custom_attribute_tag_category_taxonomy', $category_taxonomy_name, $post_type );

Pro tip: This filter can be used for any taxonomy associated with a post type. Post category or WooCommerce product category is simply a name of a taxonomy.

Usage in Image Attributes Pro

The following code is from Image Attributes Pro version 3.0 and above where the filter is used to extend %category% to WooCommerce products.

/**
 * Add WooCommerce Product category taxonomy to custom attribute tag %category%.
 * 
 * @since 3.0
 * 
 * @param $category_taxonomy_name (string) Name of the taxonomy.
 * @param $post_type (string) will have the post type of the parent post 
 * where the image is used.
 * 
 * @return $category_taxonomy_name (string) WooCommerce product category 
 * taxonomy name ('product_cat') if $post_type is 'product'.
 */
function iaffpro_add_wc_product_category_to_custom_attribute_tag_category ( $category_taxonomy_name, $post_type ) {

	// Check if post type is WooCommerce product. 
	if ( strcmp( $post_type, 'product' ) === 0 ) {
		return 'product_cat';
	}

	return $category_taxonomy_name;
}
add_filter( 'iaffpro_custom_attribute_tag_category_taxonomy', 'iaffpro_add_wc_product_category_to_custom_attribute_tag_category', 10, 2 );

Example Usage

Here is a sample code that you could use.

  • Replace custom_post_type with the name of the post type.
  • Replace custom_taxonomy with the name of the taxonomy.
  • Both these can be found from the browser address bar when you visit the Category page for the custom post type in WordPress admin. Refer to the product category screenshot above.
  • custom_post_type will be what comes after post_type=
  • custom_taxonomy will be what comes after taxonomy=
  • Both these are highlighted below.
/**
 * Add custom category taxonomy (custom_taxonomy) to custom attribute tag %category%.
 * 
 * @param $category_taxonomy_name (string) Name of the taxonomy.
 * @param $post_type (string) will have the post type of the parent post 
 * where the image is used.
 * 
 * @return $category_taxonomy_name (string) Category taxonomy name ('custom_taxonomy') 
 * if $post_type is 'custom_post_type'.
 * 
 * @link https://imageattributespro.com/codex/iaffpro_custom_attribute_tag_category_taxonomy/
 */
function prefix_iap_detect_custom_category ( $category_taxonomy_name, $post_type ) {

	// Check if post type is custom_post_type. 
	if ( strcmp( $post_type, 'custom_post_type' ) === 0 ) {
		return 'custom_taxonomy';
	}

	return $category_taxonomy_name;
}
add_filter( 'iaffpro_custom_attribute_tag_category_taxonomy', 'prefix_iap_detect_custom_category', 10, 2 );

Related: iaffpro_custom_attribute_tag_tag_taxonomy