How can I retrieve the current URL (whether homepage, archive, post type archive, category archive, etc) but always without the /page/{pagenum}/ part if it’s present? So, if the real URL is:

example.com/category/uncategorized/

OR

example.com/category/uncategorized/page/2/

then the return value will always be

example.com/category/uncategorized/

8

You can get the current URL through home_url( $wp->request ).

Try the example below:

global $wp;

// get current url with query string.
$current_url =  home_url( $wp->request ); 

// get the position where '/page.. ' text start.
$pos = strpos($current_url , '/page');

// remove string from the specific postion
$finalurl = substr($current_url,0,$pos);


echo $finalurl;

Leave a Reply

Your email address will not be published. Required fields are marked *