Adding a Brand taxonomy and displaying it in the single template
-
I have successfully created a new taxonomy for brands using the code below but can not work out how to display it in the product page.
This is how I ad the custom taxonomy
//hook into the init action and call create_book_taxonomies when it fires add_action( 'init', 'create_product_taxonomies', 0 ); //add_action('admin_init', 'flush_rewrite_rules'); //create two taxonomies, genres and writers for the post type "book" function create_product_taxonomies() { // Add new taxonomy, make it hierarchical (like categories) $labels = array( 'name' => _x( 'Brands', 'taxonomy general name' ), 'singular_name' => _x( 'Brand', 'taxonomy singular name' ), 'search_items' => __( 'Search Brands' ), 'all_items' => __( 'All Brands' ), 'parent_item' => __( 'Parent Brand' ), 'parent_item_colon' => __( 'Parent Brands:' ), 'edit_item' => __( 'Edit Brands' ), 'update_item' => __( 'Update Brands' ), 'add_new_item' => __( 'Add New Brand' ), 'new_item_name' => __( 'New Brand Name' ), 'menu_name' => __( 'Brands' ), ); register_taxonomy('product_brand',array('product'), array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'query_var' => true, //'rewrite' => true, 'rewrite' => array( 'slug' => 'brands', 'with_front' => true ), ));
function filter_post_type_link($link, $post) { if ($post->post_type != 'product') return $link; if ($cats = get_the_terms($post->ID, 'product_brand')) if ($cats = get_the_terms($post->ID, 'product_brand')) $link = str_replace('%product_brand%', array_pop($cats)->slug, $link); return $link; } add_filter('post_type_link', 'filter_post_type_link', 10, 2);
All good so far and can add my brands as I add a product.
To try to add it to the template I have found the following in meta.php
<?php echo $product->get_categories( ', ', ' <span class="posted_in">'.__('Category:', 'woocommerce').' ', '.</span>'); ?> <?php echo $product->get_tags( ', ', ' <span class="tagged_as">'.__('Tags:', 'woocommerce').' ', '.</span>'); ?>
and found the code for the get_categories array in woocommerce_template.php but am not sure what to copy and change to use it.
// NOTE: using child_of instead of parent - this is not ideal but due to a WP bug ( https://core.trac.www.ads-software.com/ticket/15626 ) pad_counts won't work $args = array( 'child_of' => $product_category_parent, 'menu_order' => 'ASC', 'hide_empty' => 1, 'hierarchical' => 1, 'taxonomy' => 'product_cat', 'pad_counts' => 1 ); $product_categories = get_categories( $args ); $product_category_found = false; if ( $product_categories ) { foreach ( $product_categories as $category ) { if ( $category->parent != $product_category_parent ) continue; if ( ! $product_category_found ) { // We found a category $product_category_found = true; echo $before; } woocommerce_get_template( 'content-product_cat.php', array( 'category' => $category ) ); } }
Appreciate your help
charlotteswebpapercraft.com.au
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Adding a Brand taxonomy and displaying it in the single template’ is closed to new replies.