Skip to content

Instantly share code, notes, and snippets.

@unnamedfeeling
Created October 6, 2017 21:46
Show Gist options
  • Save unnamedfeeling/03befb854fdc601a56302759fe2f0592 to your computer and use it in GitHub Desktop.
Save unnamedfeeling/03befb854fdc601a56302759fe2f0592 to your computer and use it in GitHub Desktop.
Ajax get posts with js
function generic_ajax_posts(){
$params=$_POST
$offset=$params['params']['p']*((!empty($params['params']['q'])) ? $params['params']['q'] : 4);
$post_type=$params['params']['pt'];
$catid=(!empty($params['params']['cat']))?$params['params']['cat']:null;
$tpl=(!empty($params['params']['tpl']))?$params['params']['tpl']:null;
$args=array(
'post_type'=>$post_type,
'posts_per_page'=>4,
'offset'=>$offset
);
if(!empty($params['params']['order'])) $args['order']=$params['params']['order'];
if(!empty($params['params']['orderby'])) $args['orderby']=$params['params']['orderby'];
if(!empty($catid)){
$args['tax_query']=array(
array(
'taxonomy'=>'product_cat',
'terms'=>array($catid)
)
);
}
$posts=new WP_Query($args);
ob_start();
if ($posts->have_posts()): while ($posts->have_posts()) : $posts->the_post();
if(!empty($tpl)){
get_template_part('loop', $tpl);
} else {
get_template_part('loop');
}
endwhile;
else : ?>
<div class="noposts" style="width:100%;font-size:2rem;font-weight:bold;"><?=__( 'No more swimsuits!', 'generic' )?></div>
<?php endif;
// die(json_encode($args));
$cont=ob_get_clean();
$response['offset']=$offset;
$response['total_posts']=wp_count_posts($post_type)->publish;
$response['content']=$cont;
// $response['params']=$params;
die(json_encode($response));
}
var ajaxPost = function(set){
// target, el, type, container, func, quant, callback, event, tplt
var event=(typeof set.event !== 'undefined') ? set.event : null,
q=(typeof set.quant!=='undefined') ? set.quant : 4,
cont=document.getElementById(set.container),
page = parseInt(cont.dataset.page),
cat = (!isNaN(cont.dataset.cat))?parseInt(cont.dataset.cat):'',
catid = (!isNaN($(set.target).get(0).dataset.cat))?parseInt($(set.target).get(0).dataset.cat):'',
tagid = (!isNaN($(set.target).get(0).dataset.tagid))?parseInt($(set.target).get(0).dataset.tagid):'',
tpl=(set.tplt!=='undefined'&&set.tplt!=='')?set.tplt:null,
postData={
p: page,
c: ((cat!=='') ? cat : catid),
pt: set.type,
q: q,
t: tagid,
postid: ((set.container=='commentContainer')?commentContainer.dataset.pid:null),
o: $(cont).find(set.el).length,
tpl: tpl,
order: set.order,
orderby: set.orderby
},
postcount=parseInt($('#'+set.container).find('.grid-item').length),
result;
$.post({
url: ajax_func.ajax_url,
dataType: 'json',
data: {
action: set.func,
params: postData
},
success: function (data) {
// console.log(data);
// if($('#'+container).find('.noposts').length){
// $('#'+container).find('.noposts').remove();
// }
result=data;
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.responseText);
}
}).done(function(){
// console.log(typeof callback);
// return result;
if(typeof set.callback!=='undefined'&&set.callback!==''){
// console.log(callback);
set.callback(result, set.event);
}
});
},
moreRev=function(data, event){
// console.log(data);
$(event.target).attr('disabled', 'disabled').css('opacity', '0.7');
var ajaxCont=$.parseHTML(data.content),
postcount,
totalposts=parseInt(data.total_posts);
$('#commentContainer').append(ajaxCont);
commentContainer.dataset.page++;
postcount=$('#commentContainer').find('.comment').length;
if(parseInt(postcount)==parseInt(totalposts)||parseInt(postcount)>(parseInt(totalposts)-1)){
$(event.target).addClass('hidden').hide(300);
} else {
$(event.target).removeAttr('disabled').css('opacity', '1');
}
};
$(document).on('click', '.js-revMore', function(event){
// target, el, type, container, func, quant, callback, event, tplt
var set={
target: '.js-revMore',
el: '.comment',
type: 'product',
container: 'commentContainer',
func: 'generic_ajax_comments',
quant: 4,
callback: moreRev,
event: event,
tplt: ''
};
ajaxPost(set);
event.preventDefault();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment