I'm trying to retrieve the current URL of the page, which works everywhere except for archive pages, for example, a category page. On a category page, it shows as the most recent post in that category.
$this_id = $wp_the_query->get_queried_object_id();
$this_url = get_permalink($this_id);
echo $this_url; // Should show like:
How do I retrieve the actual archive page URL?
I'm trying to retrieve the current URL of the page, which works everywhere except for archive pages, for example, a category page. On a category page, it shows as the most recent post in that category.
$this_id = $wp_the_query->get_queried_object_id();
$this_url = get_permalink($this_id);
echo $this_url; // Should show like: http://domain/blog/category
How do I retrieve the actual archive page URL?
Share Improve this question asked Jul 27, 2014 at 23:59 MultiDevMultiDev 1273 silver badges7 bronze badges3 Answers
Reset to default 1Untested, but here is a solution from Konstantin Kovshenin
global $wp;
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
You can then just simply echo $current_url
Category URLs can be retrieved using function get_category_link
. So instead of using get_permalink
, if you use get_category_link
and pass the category id to it, it would return the link of the category. You can read more about get_category_link
here.
Continuing the first answer, from Pieter Goosen:
Using same code but without the current category/tag/taxonomy being repeated with a string of the same taxonomy, you can do this:
<?php
global $wp;
$current_url = home_url( $wp->request ) . '/'; // last part adds the "/" at the end
?>
Then for the canonical meta (or any other permalink placement):
<link rel="canonical" href="<?php echo $current_url ?>" />
Observe, this will fail for url:s that have query strings at the end and is only for "pretty urls", like domain/category/sub-category/
.