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?
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.
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:This will return an array containing all albums this post is connected to.
You could display that array as follows:
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!
You could add a GET parameter in your theme and then pick it up on the next page. Something like this, I guess.
Then you have (don’t try this at home unsanitized data is bad for you):
or
If you want to do something with it. Other options include getting the ID or the slug both of which can be used with
get_term_by(...)
.Obviously, you’d want to clean the GET data before use but the principle of putting the data into the URL works as a simple breadcrumb system.
If you want to get fancy, you could push the current category into a cookie with a function hooking somewhere before headers are sent. The principle is the same but this GET hack is simpler (especially in the EU with cookie laws).