1

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?

1

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.

0

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!

  • 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? – xyz 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) – xyz Sep 3 '12 at 8:22
0

You could add a GET parameter in your theme and then pick it up on the next page. Something like this, I guess.

 <a href="<?php the_permalink() ?>?source=<?php single_cat_title(); ?>">

Then you have (don't try this at home unsanitized data is bad for you):

 <?php if(isset($_GET['source'])){ ?>
 <p>You came from <?php echo $_GET['source']; ?></p>
 <?php } ?>

or

 <?php
 $cat = get_term_by( 'name', $_GET['source'], 'category' );

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).

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.