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/category/uncategorized/
OR
example/category/uncategorized/page/2/
then the return value will always be
example/category/uncategorized/
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 Answers
Reset to default 14You 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;
Actually the easiest would be to use get_pagenum_link()
which will return you the current URL without any /page/*
paramaters.
Bonus
You can also simply use it to build "Previous" and "Next" links dynamically using the 'paged'
query variable:
// Get the current page number.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$first_page = '<a href="' . get_pagenum_link() . '" class="page-first"><<</a>';
$prev_page = '<a href="' . get_pagenum_link($paged - 1) . '" class="page-prev"><</a>';
$next_page = '<a href="' . get_pagenum_link($paged + 1) . ' class="page-next">></a>';
// And having a `WP_Query` object you can also build the link for the last page:
$max = $the_query->max_num_pages;
$last_page = '<a href="' . get_pagenum_link($max) . '" class="page-last">>></a>';
Answer by Govind Kumar worked, however, it only returned the URL if /page/{pagenum}/ was present in the URL and returned nothing if not. I needed a universal solution that will always return the base URL without pagination, so I modified Govind's code a bit and wrapped into a function:
function get_nopaging_url() {
global $wp;
$current_url = home_url( $wp->request );
$position = strpos( $current_url , '/page' );
$nopaging_url = ( $position ) ? substr( $current_url, 0, $position ) : $current_url;
return trailingslashit( $nopaging_url );
}
echo get_nopaging_url();
Now, it always returns correct URL.
(This is useful if you need to implement some kind of post filters that add a param to filter posts by, let's say, a meta filed. So, even if a user sets the filter param on page X, the new filtered results will always start from the base URL, not page X and throwing 404 if there's less filtered posts.)
My specifics were very similar to Ihor Vorotnov's, exept that I had more than one parameter. So, starting from his answer, I modified the code to use a regular expression:
function get_nopaging_url() {
$current_url = $_SERVER[REQUEST_URI];
$pattern = '/page\\/[0-9]+\\//i';
$nopaging_url = preg_replace($pattern, '', $current_url);
return $nopaging_url;
}
If you want to remove all possible pagination combinations then use this snippet:
// remove pagination from url
$pattern = '/page(\/)*([0-9\/])*/i';
$url = preg_replace($pattern, '', $GLOBALS['wp']->request);
It will take care of
/page
/page/
/page/1
/page/1/
...
and is case insensitive, works for any pagination page number and also will remove any combination of trailing number/number/number... (tests here: https://regex101.com/r/9fQaLC/1)
If you want to get the full url with leading http(s) just add
$url = home_url($url);
In that line you can also add any custom GET parameter like so
$url = home_url($url . '?typ=test');
function getCurrentUrlSeo() {
global $wp;
$current_url = home_url($wp->request);
$pos = mb_strpos($current_url, '/page');
$finalurl = $pos ? substr($current_url, 0, $pos) : $current_url;
return $finalurl.'/';
}
While this question was already answered here (not accepted), this snippet should do the trick: home_url(add_query_arg(NULL, NULL));
.
trait TraitURLStrip {
/**
* Strips off paging and query string. Returns a filter-the-results friendly URL
*
* @param bool $str_url
* @param string $str_page
*
* @return bool|string
*/
protected function urlStrip( $str_url = false , $str_page = 'page' ){
if ( is_string( $str_url) ) {
$arr_parse_url = wp_parse_url( $str_url );
$str_path_no_page = '';
if ( isset( $arr_parse_url['path'] ) ) {
// if we're paging then remove that. please!
$str_path_no_page = preg_replace( "/\/{$str_page}\/[0-9]*\/$/", "/", $arr_parse_url['path'] );
}
$str_scheme_host = "{$arr_parse_url['scheme']}://{$arr_parse_url['host']}";
return $str_scheme_host . $str_path_no_page;
}
return false;
}
}