Skip to content

Instantly share code, notes, and snippets.

@aliboy08
Last active October 8, 2018 01:29
Show Gist options
  • Save aliboy08/79e647c5f0d69dacc000818800d34def to your computer and use it in GitHub Desktop.
Save aliboy08/79e647c5f0d69dacc000818800d34def to your computer and use it in GitHub Desktop.
Wordpress js pagination hack fix if rewrite is not working
// Pagination fix
if( $('.wp-pagenavi').length ) {
var url, match, find_page_var, page_var, split_page_var, new_page_parameter, new_url;
$('.wp-pagenavi a').each(function(){
url = $(this).attr('href');
// get the page parameter from the url
match = /\/page\/[0-9]+\//g.exec(url);
if( match ) {
page_var = match[0];
split_page_var = page_var.split('/');
if( url.indexOf('?') > -1 ) {
// have query strings
new_page_parameter = '&'+ split_page_var[1] + '=' + split_page_var[2];
if( url.indexOf('page=') > -1 ) {
// page parameter already exists
new_url = url.replace(page_var, '');
var match_2 = /page=[0-9]+/g.exec(new_url);
// only replace the page= value
new_url = new_url.replace(match_2[0], 'page='+ split_page_var[2]);
} else {
// build the new url with the replaced &page=NUM
new_url = url.replace(page_var, '') + new_page_parameter;
}
} else {
// no existing parameters,
// build the new url with the replaced ?page=NUM
new_page_parameter = '?'+ split_page_var[1] + '=' + split_page_var[2];
new_url = url.replace(page_var, '') +'/'+ new_page_parameter;
}
// assign the new url to the link
$(this).attr('href', new_url);
}
if( $(this).text() == '1' ) {
// Page 1, replace page=NUM with 1
match = /page=[0-9]+/g.exec(url);
if( match ) {
$(this).attr('href', url.replace(match[0], '') + 'page=1');
}
}
})
} // Pagination fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment