Skip to content
Advertisement

WooCommerce: Get URL of paged shop archive

I need the link to the paged shop archive URL to add it as rel=canonical. As default WooCommerce seems to use always the URL of the “All products” page.

For example: The all product archive page has the following URL:

https://example.com/shop/

If I go to page 2, the URL is still the one from above. But the correct version would be:

https://example.com/shop/page/2/

I try to rewrite the rel=canonical tag with Yoast and want to display the current URL with /page/. So I tried this:

$canonical = home_url( $wp->request );

And this:

$canonical = home_url( add_query_arg( array(), $wp->request ) );

Unfortunately without success. It always shows only the URL of homepage (https://example.com) Is there any way to get the number of the page?

Advertisement

Answer

OK, found a solution for that. It seems that $wp->request doesn’t work on the WooCommerce shop archive.

Here’s a solution that works. But it has some hard coded URL parts in it:

$canonical = get_permalink(woocommerce_get_page_id( 'shop' )).'page/'.get_query_var('paged').'/';

I wonder if there is a better solution without adding /page/ to the URL.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement