Skip Navigation

[Résolu] Output Taxonomy Archive URL Inside an Archive

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
How to output the URL of the current taxonomy archive?

Solution:
You would need to register a custom shortcode, which does not need any attributes, simply returning the URL from the $_SERVER PHP object:

add_shortcode( 'current-url', function(){
 
    return $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
 
});
This support ticket is created Il y a 5 années et 6 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 2 réponses, has 2 voix.

Last updated by Thomas AMX Il y a 5 années et 6 mois.

Assisted by: Nigel.

Auteur
Publications
#1130838

Hi Guys,

I am building lightweight, custom social sharing buttons and I would like to make taxonomy archive pages shareable. So, I need to generate the archive's URL and it turns out to to be unexpectedly tricky.

[wpv-taxonomy-url] seemed like an obvious choice at first, but it won't work inside taxonomy archives....
[wpv-taxonomy-archive info=""] works inside tax. loops, but doesn't have URLs as option... and using slugs won't work for hierchical taxonomies.

I found this shortcode written by Nigel:
https://toolset.com/forums/topic/displaying-view-by-taxonomies-url-in-the-archieve-loop/#post-1121600
It works but it also doesn't have URL as an option.

I have tried to 'reverse-engineer' it with : "if ($output = 'url')" and "get_term_link".
I suspect I was on the right track, but I don't really know PHP and so I have failed 🙂

Is it possible to adapt the shortcode so that it can output the archive's URL or perhaps there is another, simpler way to achieve it?

Cheers,
Tom

#1131862

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Hi Tom

The archive URL isn't available with the query object used by the taxonomy shortcode you linked to, you should probably get it directly from the server request variables.

Try adding the following custom shortcode to output the current URL:

add_shortcode( 'current-url', function(){

    return $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

});
[php]

You can then use

[php]
[current-url]

to output the URL where required. You can manually include the protocol (e.g. hidden link) if you require it.

#1132984

Thank you very much, Nigel, it works!
What's cool about this code is that I would be able to use it in other contexts as well.
Excellent.

Cheers,
Tom

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.