WordPress current URL Cheat Sheet

Input

https://juzhax.com/page-name/page/3/?query=data

Currrent URL with Pagination and Query String

$final_url = "{$parts['scheme']}://{$parts['host']}" . add_query_arg( NULL, NULL );

// Result : https://juzhax.com/page-name/page/3/?query=data

Current URL without Query String and ending with slash

$current_url =  home_url( $wp->request );
$final_url = trailingslashit( $current_url );

or 

$final_url = get_permalink();

or 

$final_url = get_page_link(); // If it is page.


// Result : https://juzhax.com/page-name/

Current URL with Pagination and removed Query String, good for canonical url

global $wp;
$current_url =  home_url( $wp->request );
$position = strpos( $current_url , '/page' );
$nopaging_url = ( $position ) ? substr( $current_url, 0, $position ) : $current_url;
$final_url = trailingslashit( $nopaging_url );

// Result: https://juzhax.com/page-name/page/3/

Next Page URL from current URL

$next_page_url = get_next_posts_page_link();
// Result: https://juzhax.com/page-name/page/4/

Previous Page URL from current URL

$next_page_url = get_previous_posts_page_link();
// Result: https://juzhax.com/page-name/page/2/

Tags:

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.