Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coop920/c057da0510f18ce01bc0ef15736131f7 to your computer and use it in GitHub Desktop.
Save Coop920/c057da0510f18ce01bc0ef15736131f7 to your computer and use it in GitHub Desktop.
/**
* Get posts via ajax and WP REST API.
* @param {object} options
*/
_blogLoaded = false;
function getAjaxPosts(args) {
var $wrapper = $("#postsWrapper"),
context = $wrapper.attr("data-context"),
options = typeof args === "undefined" ? SAGE_ARCHIVE_VARS : args,
url = SAGE_AJAX_URL.ajaxurl + "wp-json/sugar/v1/" + context + "/?";
// append options as params to url
url += $.param(options);
/*
* Make ajax request to WP endpoint.
* This is where most everything happens.
*/
$.getJSON(url, function (data, status, xhr) {
// get response headers for pagination
totalPages = xhr.getResponseHeader("x-wp-totalpages");
totalPosts = xhr.getResponseHeader("x-wp-total");
// loop over each result and append to our wrapper
$(data).each(function () {
$wrapper.append(this.html);
totalReturned++;
});
}).done(function() {
if( Object.keys(SAGE_ARCHIVE_VARS).length < 3 ) {
var current_page = SAGE_ARCHIVE_VARS.paged;
var url = 'https://' + window.location.hostname;
var pathname = window.location.pathname;
if( '' !== pathname ) {
pathname = pathname.split('/');
console.log(pathname);
var root = pathname[1];
}
//console.log( window.location );
if( 1 === current_page ) {
url += '/' + root + '/';
} else {
url += '/' + root + '/page/' + current_page + '/';
}
if( true === _blogLoaded ) {
if (history.pushState) {
history.pushState({}, "Blog", url );
}
//console.log( 'update URL to ' + url );
} else {
_blogLoaded = true;
}
} else {
pathname = window.location.pathname;
if( '' !== pathname ) {
pathname = pathname.split('/');
//console.log(pathname);
var root_new = pathname[1];
}
//console.log( window.location );
var url_new = window.location.origin + '/' + root_new + '/' + window.location.search;
history.pushState({}, "Blog", url_new );
//console.log( 'category update URL to ' + url_new );
}
// check if we should show load more button
$("#loadMoreBtn")
.attr("data-finished", totalReturned >= totalPosts)
.prop("disabled", false)
.text(function () {
return $(this).data("text");
});
// turn off ajax loader
$("#ajaxLoader").addClass("d-none");
if (parseInt(totalPosts) > 0) {
var i = 0;
var resultsQuery = "";
$.each(options, function (index, item) {
if (index === "posts_per_page" || index === "paged") {
if (!$("#showingResults").hasClass("d-none")) {
$("#showingResults").addClass("d-none");
}
} else {
$("#showingResults").removeClass("d-none");
$("#showingResults .result-number").html(parseInt(totalPosts));
if (parseInt(totalPosts) > 1) {
if ($("#showingResults .plural").hasClass("d-none")) {
$("#showingResults .plural").removeClass("d-none");
}
if (!$("#showingResults .singular").hasClass("d-none")) {
$("#showingResults .singular").addClass("d-none");
}
} else {
if ($("#showingResults .singular").hasClass("d-none")) {
$("#showingResults .singular").removeClass("d-none");
}
if (!$("#showingResults .plural").hasClass("d-none")) {
$("#showingResults .plural").addClass("d-none");
}
}
var addArray = item.split(",");
$.each(addArray, function (index, item) {
if (i > 0) {
resultsQuery += ", ";
}
resultsQuery += '"';
var add = item.replace("-", " ");
resultsQuery += add;
resultsQuery += '"';
i++;
});
}
});
$("#showingResults .resultterms").html(resultsQuery);
} else {
if (!$("#showingResults").hasClass("d-none")) {
$("#showingResults").addClass("d-none");
}
}
// result-terms
// if no posts are returned, add 'no posts found' message
if (parseInt(totalPosts) === 0) {
$wrapper.append(
'<div class="col-12"><div class="alert alert-warning">No results found. Try resetting your filters.</div></div>'
);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment