FacetWP – generate prev/next buttons from cookie data

mgibbs189

{{gist file=”test.js” lang=”javascript”}}
<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>
{{/gist}}