0
//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!

  • What code are you using to display these? And what error are you getting? – RiddleMeThis Sep 18 '18 at 12:44
1

get_the_terms() returns FALSE if there are no terms in the taxonomy, so you could check for that.

$authors = get_the_terms( $post->ID , 'product_author' );
if ( FALSE != $authors ) :
    // Do something
endif;

If you want to avoid displaying empty terms, ie. terms that have no objects attached to them, use get_terms() instead, because you can pass a parameter to filter them:

$terms = get_terms( 
   array(
     'object_ids' => $post->ID,
     'taxonomy' => 'product_author',
     'hide_empty' => TRUE,
) );
0

The error message I was getting was an invalid foreach statement when the array was empty.

So the quick solve for me was to just put in a check that the array wasn't empty before executing anything.

if (!empty ($authors)) {

Thanks for the input, really appreciate it!

If I manage to tidy it up at all I'll post the full code later.

Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.