1

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.com/blog/category

How do I retrieve the actual archive page URL?

3 Answers 3

1

Untested, 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

0
0

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.

0

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.com/category/sub-category/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.