//Register new product taxonomy - Authors
function register_author_taxonomy() {
$labels = array(
'name' => __( 'Author', 'taxonomy general name' ),
'singular_name' => __( 'Author', 'taxonomy singular name' ),
'search_items' => __( 'Search Authors' ),
'all_items' => __( 'All Authors' ),
'parent_item' => __( 'Parent Author' ),
'parent_item_colon' => __( 'Parent Author:' ),
'edit_item' => __( 'Edit Author' ),
'update_item' => __( 'Update Author' ),
'add_new_item' => __( 'Add New Author' ),
'new_item_name' => __( 'New Author Name' ),
'menu_name' => __( 'Authors' ),
);
$args = array(
'public' => true,
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'authors',
'with_front' => true,
),
'show_admin_column' => true
);
register_taxonomy("product_author", array("product"), $args );
}
add_action( 'init', 'register_author_taxonomy' );
// Register new product taxonomy - Publisher
function register_publisher_taxonomy() {
$labels = array(
'name' => __( 'Publisher', 'taxonomy general name' ),
'singular_name' => __( 'Publisher', 'taxonomy singular name' ),
'search_items' => __( 'Search Publishers' ),
'all_items' => __( 'All Publishers' ),
'parent_item' => __( 'Parent Publisher' ),
'parent_item_colon' => __( 'Parent Publisher:' ),
'edit_item' => __( 'Edit Publisher' ),
'update_item' => __( 'Update Publisher' ),
'add_new_item' => __( 'Add New Publisher' ),
'new_item_name' => __( 'New Publisher Name' ),
'menu_name' => __( 'Publishers' ),
);
$args = array(
'public' => true,
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'publishers',
'with_front' => true,
),
'show_admin_column' => true
);
register_taxonomy("product_publisher", array("product"), $args );
}
add_action( 'init', 'register_publisher_taxonomy' );
// Add Custom Taxonomies to Product Pages
add_action('woocommerce_single_product_summary',
'custom_taxonomy_description', 10 );
function custom_taxonomy_description() {
if (is_product) {
$authors = get_the_terms( $post->ID , 'product_author' );
foreach ( $authors as $author ) {
$author_link = get_term_link( $author, 'product_author' );
$publishers = get_the_terms( $post->ID , 'product_publisher' );
foreach ( $publishers as $publisher ) {
$publisher_link = get_term_link( $publisher, 'product_publisher' );
if( is_wp_error( $author_link, $publisher_link ) )
continue;
echo '<br>'. '<div class="before-content">'. 'Author: '.'<a href="' . $author_link . '">' . $author->name . '</a>'. '<br>'. 'Publisher: '.'<a href="' . $publisher_link . '">' . $publisher->name . '</a>'.'</div>'. '<br>';
}
I'm trying to add some custom taxonomies to my woocommerce products, essentially a book shop so I want author, publisher etc. I'd like these displayed on the product page all of which the above code is doing, however when either custom taxonomy is empty I get an error messaged displayed.
Just wondering if anyone had some sort of if ( ! empty () ) solution that would display nothing when the taxonomy is empty.
The code I'm using is pretty hobbled together from lots of other different posts so any ideas to streamline that would be appreciated too!
Also, not sure if this is relevant or not but I'm using the Genesis Framework on Wordpress.
Thanks in advance!