3

I've read all the posts about displaying terms of a custom taxonomy and it's still not working. Ok, so here's the code:

<?php
//part of template-offer.php
$taxonomy = 'brand';
$term_args=array(
  'hide_empty' => false,
  'orderby' => 'name',
  'order' => 'ASC'
);
$tax_terms = get_terms($taxonomy,$term_args);
echo $tax_terms;
?>

And on the output I'm only getting an empty array. I'm staring at the code and can't find out why. I have both my custom post and taxonomy working, created some posts and terms:

<?php
//creating a custom taxonomy called 'brand'
add_action( 'init', 'create_custom_taxonomies', 0 );
function create_custom_taxonomies() {
    $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 Brand:' ),
        'edit_item'         => __( 'Edit Brand' ),
        'update_item'       => __( 'Update Brand' ),
        'add_new_item'      => __( 'Add New Brand' ),
        'new_item_name'     => __( 'New Brand Name' ),
        'menu_name'         => __( 'Brand' ),
    );
    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'brand' ),
    );
    register_taxonomy( 'brand', null , $args );
}


//creating a custom post type that is using 'brand' taxonomy
function create_post_type() {
    register_post_type( 'products',
        array(
        'labels' => array(
            'name' => __( 'Products' ),
            'singular_name' => __( 'Product' ),
            'add_new' => _x('Add New', 'products'),
            'add_new_item' => __('Add New Product'),
            'edit_item' => __('Edyt Product'),
            'new_item' => __('New Product'),
            'view_item' => __('View Product'),
            'search_items' => __('Search Product'),
            'not_found' =>  __('Nothing found'),
        ),
        'taxonomies' => array('brand'),
        'supports' => array('title', 'editor', 'thumbnail'),
        'public' => true,
        'has_archive' => true,
        'show_ui' => true,
        )
    );

}
add_action( 'init', 'create_post_type' );
?>

Your help will be highly appreciated. Have a nice day, Patryk

0

1 Answer 1

5

get_terms returns an array of objects. You cannot echo an array, if you do, you will just get Array(). What you can do is print_r($array) or var_dump($array) or echo json_encode($array) to see the data it contains.

Otherwise, to get single elements, e.g. the name, from the objects, you need to pass $tax_terms through a foreach loop to get the objects from each term and then from there you can also then echo the specific object you are after

Something like this will work

$tax_terms = get_terms( $taxonomy, $args );

foreach ( $tax_terms as $tax_term ) {

    echo $tax_term->name;

}
4
  • Hope you don't mind, deleted my comment and added the additional information to your answer. Commented Jan 17, 2015 at 22:38
  • No problem, saw your comment came through as I submitted my answer. :-) Commented Jan 17, 2015 at 22:43
  • Yea, I kind of was thinking it is a PHP problem, so I didn't wanted to do an answer. But your answer brings it back to an WP specific element. Commented Jan 17, 2015 at 22:45
  • I'm pretty sure I tried such a foreach loop and it didn't work. Anyways - thanks, you saved my day!
    – bypatryk
    Commented Jan 17, 2015 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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