{"id":2591,"date":"2022-03-22T13:51:21","date_gmt":"2022-03-22T08:21:21","guid":{"rendered":"https:\/\/imageattributespro.com\/?post_type=codex&p=2591"},"modified":"2023-05-05T16:22:38","modified_gmt":"2023-05-05T10:52:38","slug":"iaffpro_custom_attribute_tag_category_taxonomy","status":"publish","type":"codex","link":"https:\/\/imageattributespro.com\/codex\/iaffpro_custom_attribute_tag_category_taxonomy\/","title":{"rendered":"iaffpro_custom_attribute_tag_category_taxonomy"},"content":{"rendered":"\n

Filter the taxonomy name for %category%<\/code> custom attribute tag to extend it to categories of other post types. By default %category%<\/code> fetches the category of the WordPress post<\/code> post type. <\/p>\n\n\n\n

The taxonomy name for the WordPress post<\/code>‘s category is category<\/code> and that of WooCommerce product category is product_cat<\/code>.<\/p>\n\n\n

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

Pro tip:<\/strong> 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. <\/p>\n\n\n\n

Usage in Image Attributes Pro<\/h2>\n\n\n\n

The following code is from Image Attributes Pro version 3.0 and above where the filter is used to extend %category%<\/code> to WooCommerce products. <\/p>\n\n\n\n

\/**\n * Add WooCommerce Product category taxonomy to custom attribute tag %category%.\n * \n * @since 3.0\n * \n * @param $category_taxonomy_name (string) Name of the taxonomy.\n * @param $post_type (string) will have the post type of the parent post \n * where the image is used.\n * \n * @return $category_taxonomy_name (string) WooCommerce product category \n * taxonomy name ('product_cat') if $post_type is 'product'.\n *\/\nfunction iaffpro_add_wc_product_category_to_custom_attribute_tag_category ( $category_taxonomy_name, $post_type ) {\n\n\t\/\/ Check if post type is WooCommerce product. \n\tif ( strcmp( $post_type, 'product' ) === 0 ) {\n\t\treturn 'product_cat';\n\t}\n\n\treturn $category_taxonomy_name;\n}\nadd_filter( 'iaffpro_custom_attribute_tag_category_taxonomy', 'iaffpro_add_wc_product_category_to_custom_attribute_tag_category', 10, 2 );<\/code><\/pre>\n\n\n\n

Example Usage<\/h2>\n\n\n\n

Here is a sample code that you could use. <\/p>\n\n\n\n