0

I use Custom Taxonomy "Artists" to differentiate between "Albums" (Parent Taxonomy) and "Albums" (belonging to an Artist, Child Taxonomy).

I am showing my taxonomy on posts for users to see which Artist and Album is associated with each post. My code is working fine in some cases, but in others, it doesn't differentiate correctly between Parent (Artists) and Child (Album) (Children show as Parents). Here is what I'm using at the moment. How can I improve it to avoid my issue?

$_terms = wp_get_post_terms($post->ID, 'hhie_artists');
   if (!empty ($_terms)):
      echo "In this Article: ";
      echo "Artist/s: ";
foreach ($_terms as $_term) {
   if ($_term->parent == 0) //check for parent terms only
      '<a href=' . get_term_link($_term) . '>' . $_term->name . '</a> | ';

   if ($_term->parent > 0) //check for children
      echo 'Album: <a href=' . get_term_link($_term) . '>' . $_term->name . '</a>';
   }
endif;`

Note: There will be a max of one album per post.

EDIT: Sorry for lack of clarity. I get things like this returned:
Artist/s: Album: A Year In The Life Of Oscar The SlouchRamson Badbonez | (Should be: Artist/s: Ramson Badbonez | Album: A Year In The Life Of Oscar The Slouch) This doesn't always happen though. Sometimes it shows correctly. In the case of multiple artists: Artist/s: Album: Dabbla JehstHeavy Ear Play (Should be: Artist/s: Dabbla Jehst | Album: Heavy Ear Play)

  • This is a little hard to follow. Could you maybe add specific examples of your logic working and not working? – Rarst Dec 11 '13 at 14:10
  • I have the suspicion this is about the order inside your return/result from the wp_get_post_terms call. – Nicolai Dec 11 '13 at 14:17
0

If I were doing a site such as you describe, I would probably make a Custom Post Type for "Artists", another Custom Post Type for "Albums", and would link them with the "Posts 2 Posts" plugin.

It's easy to use and well documented.

That way, you can later easily attach images and galleries, mp3s, and whatnot. Using taxonomies has many limitations.

And you could add a "genre" or "styles" taxonomy, to tag your artists and albums with.

Just my 2 cents :)

  • And if I converted my Custom Taxonomy to a Custom Post Type to go down the route you suggest, how would I go about transferring the Custom Taxonomy associations in my posts and convert my CT Meta Fields to Custom Post Type Custom Fields i.e. to not lose any data? – JMB Dec 11 '13 at 23:49
  • Good question. If you have a large number of taxonomy items to convert, I guess I would write a template that exports them all as a CSV table, then re-import these into a Custom Post Type using a CSV-Import plugin. Then try to create the connections programmatically, using this API: github.com/scribu/wp-posts-to-posts/wiki/… - but I never did this, always used p2p starting from zero content. – Manu Dec 16 '13 at 11:20

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.