Skip Navigation

[Resolved] Output Taxonomy Archive URL Inside an Archive

This thread is resolved. Here is a description of the problem and solution.

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 6 years, 1 month ago. 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.

Our next available supporter will start replying to tickets in about 2.43 hours from now. 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+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by Thomas AMX 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#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: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00: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