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 badges3 Answers
Reset to default 1If 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
版权声明:本文标题:posts - Pass data between pages 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745420128a2657863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论