Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Created September 29, 2015 15:29
Show Gist options
  • Select an option

  • Save mgibbs189/9a88ee71d0ec29bb753e to your computer and use it in GitHub Desktop.

Select an option

Save mgibbs189/9a88ee71d0ec29bb753e to your computer and use it in GitHub Desktop.
FacetWP - generate prev/next buttons from cookie data
<div class="nav-wrapper"><!-- placeholder --></div>
<script>
(function($) {
$(function() {
var post_id = <?php echo $post->ID; ?>; // grab the current post ID
var data = getCookie('fwp_posts'); // get the cookie
if ('' != data) {
data = JSON.parse(data);
var pos = data.indexOf(post_id);
if (-1 < pos) {
if (pos > 0) { // add a "Prev" link
var prev_id = data[pos - 1];
$('.nav-wrapper').append('<a href="/?p=' + prev_id + '">Prev</a>');
}
else if (pos < (data.length - 1)) { // add a "Next" link
var next_id = data[pos + 1];
$('.nav-wrapper').append('<a href="/?p=' + next_id + '">Next</a>');
}
}
}
});
})(jQuery);
// http://www.w3schools.com/js/js_cookies.asp
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment