Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Get Custom Taxonomies as links inside loop WordPress

  • SOLVED

Hello again,

I've created a custom post type for my wordpress site called 'download'

And also hierarchical taxonomy called 'group' and a non hierarchical taxonomy called 'meta'.

This is all A-OK it seems.

See functions PHP for the above here [[LINK href="http://goo.gl/vySxz"]]http://goo.gl/vySxz[[/LINK]]

See below just my taxonomies code...


// Downloads Taxonomies
add_action( 'init', 'create_download_taxonomies', 0 );
function create_download_taxonomies()
{
// Hierarchical Like Categories - group
$labels = array(
'name' => _x( 'Grouping', 'taxonomy general name' ),
'singular_name' => _x( 'Group', 'taxonomy singular name' ),
'search_items' => __( 'Search Groups' ),
'all_items' => __( 'All Groups' ),
'parent_item' => __( 'Parent Group' ),
'parent_item_colon' => __( 'Parent Group:' ),
'edit_item' => __( 'Edit Group' ),
'update_item' => __( 'Update Group' ),
'add_new_item' => __( 'Add New Group' ),
'new_item_name' => __( 'New Group Name' ),
'menu_name' => __( 'Group' ),
);

register_taxonomy('group',array('download'), array(
'hierarchical' => true,
'label' => 'Groups',
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'public'=>true,
'rewrite' => array( 'slug' => 'group' ),
));

// Non Hierarchical Like Tags - meta
$labels = array(
'name' => _x( 'Meta', 'taxonomy general name' ),
'singular_name' => _x( 'Meta', 'taxonomy singular name' ),
'search_items' => __( 'Search Meta' ),
'popular_items' => __( 'Popular Meta' ),
'all_items' => __( 'All Meta' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Meta' ),
'update_item' => __( 'Update Meta' ),
'add_new_item' => __( 'Add New Meta' ),
'new_item_name' => __( 'New Meta Name' ),
'separate_items_with_commas' => __( 'Separate meta with commas' ),
'add_or_remove_items' => __( 'Add or remove meta' ),
'choose_from_most_used' => __( 'Choose from the most used meta' ),
'menu_name' => __( 'Meta' ),
);

register_taxonomy('meta','download',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'meta' ),
));
}



But I'm styling and creating custom archive and single templates for this custom 'download' post type.

My question is, on my archives page, inside the loop, I'm trying to list the taxonomies (group and meta) used for the current post. But I need them to be links, to work in the same way as tags and category links work.

This is what I'm using so far for 'group', but it does not work as it returns all the 'group' taxonomies in this custom post type 'download'.


<?php

$taxonomy = 'group';
$terms = get_terms( $taxonomy, '' );
if ($terms) {
foreach($terms as $term) {
echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a>';
}
}

?>


Can any one help? Let me know if you need me to explain more.
Fully working code, or near enough will get prize moneys.

Thanks

Answers (3)

2011-12-20

Luis Abarca answers:

Use this functions <strong>[[LINK href="http://codex.wordpress.org/Function_Reference/the_terms"]]the_terms[[/LINK]]</strong> or <strong>[[LINK href="http://codex.wordpress.org/Function_Reference/get_the_term_list"]]get_the_term_list[[/LINK]]</strong>

So in the loop:

<?php the_terms($post->ID, 'group') ?>


Josh Cranwell comments:

Thank you!!!

2011-12-20

juan manuel incaurgarat answers:

you probably should be using wp_get_post_terms()
which goes just like this:
<?php $terms = wp_get_post_terms( $post_id, $taxonomy, $args ) ?>

$args is optional (and so is $taxonomy but you need it on this case)


Josh Cranwell comments:

Also thank you...

2011-12-20

designchemical answers:


$terms = get_the_terms($post->ID, 'meta');

if ($terms && ! is_wp_error($terms)) :

foreach ($terms as $term){

// Add you links code ... e.g
$links .= '<a href="$term->slug"></a>;
}
endif;


Josh Cranwell comments:

Interesting method but the option above seemed to do the trick. Thanks