admin管理员组

文章数量:1426918

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?

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 edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Sep 1, 2012 at 8:08 zigzagzigzag 793 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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!

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

本文标签: postsPass data between pages