Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How would you solve this?

On a post page (single.php) I need to echo out name of taxonomy that lead to it. So if I browse a list of posts from certain taxonomy and click on a post, I need on that post page (single.php) to show the previously viewed taxonomy name, that lead to post page.

taxonomy-albums.php

taxonomy-songs.php

single.php

Is there a way without using GET, POST or COOKIE method?

share|improve this question

2 Answers 2

A wordpress post can display to which taxonomies it belongs using the wp_get_object_terms() function.

For example if you want a single post to display to which terms in the album taxonomy it belongs, you'd use:

wp_get_object_terms( $post->ID, 'album' );

This will return an array containing all albums this post is connected to.

You could display that array as follows:

// Load all albums for this post into a variable
$albums = wp_get_object_terms( $post->ID, 'album' );

// Loop through all albums and print their names
foreach( $albums as $album ){
    echo $album->name; 
}

More information can be found on the wp_get_object_terms documentation page.

Of course in your case, it could be that you'd be better off creating a custom post type that represents songs, and interconnect that using the album taxonomy.

Let me know if this helps!

share|improve this answer
    
Thanks for helping Mark. Can you tell me more about "interconnect that using the album taxonomy"? How would I do that and what would I achieve with that? –  zigzag Sep 1 '12 at 9:26
    
wp_get_object_terms does not help, it just prints out the taxonomy terms associated with post. –  zigzag Sep 1 '12 at 9:34
    
Yes indeed. You could use this function to check whether it belongs to the taxonomy 'albums', but it seems like you're looking for something else. Can you provide some more clarification on what you want it to do? –  Mark Lindhout Sep 3 '12 at 8:21
    
And by 'interconnecting' I mean that you can display a couple of links on the post which link to e.g. albums to which it belongs. That way yo can browse through your content using those taxonomies, just like you would with tags. (I hope this is clear) –  Mark Lindhout Sep 3 '12 at 8:22

If there is a referrer ($_SERVER['HTTP_REFERER'], it's not the best but could do the job here), you can retrieve the taxonomies as Mark proposed and inside the loop you display only the on corresponding to the referrer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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