WordPress load post by AJAX
function get_new() {
// args
$args = array(
'post_type' => 'product',
'showposts' => 100,
'meta_query' => array(
array(
'key' => 'new',
'value' => '1',
'compare' => '=='
)
)
);
// query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
if ( has_post_thumbnail()) {
$post_thumbnail_id = get_post_thumbnail_id( $post );
$image = '<img src="'. wp_get_attachment_image_url( $post_thumbnail_id, "medium" ) .'" title="'. get_the_title() .'" alt="'. get_the_title() .'" />';
} else {
$image = '<img src="'. catchFirstImage() .'" title="'. the_title() .'" alt="'. the_title() .'" />';
}
if( get_field('sale') ) {
$action = '<span class="action upper fira-bold">НА АКЦИИ</span>';
} else {
$action = '';
}
$output = $output .'<div id="post-'. get_the_ID() .'" class="col-xs-6 col-md-4 nopadding card-product__img card-product__img--cat">
<a href="'. get_the_permalink() .'">'. $image .'
'. $action .'
<span class="num upper fira-bold">'. get_the_title() .'</span>
<i class="ic ic-loop"></i>
</a>
<a href="#" class="like" like-id="'. get_the_ID() .'"><i class="ic ic-like"></i></a>
</div><!-- end card-product__img -->';
endwhile;
endif;
// Reset Query
wp_reset_query();
die($output);
}
add_action('wp_ajax_get_new', 'get_new');
add_action('wp_ajax_nopriv_get_new', 'get_new'); // not really needed
<script type="text/javascript">
$(document).ready(function() {
$(".sidebar-links--new").click(function(e) {
e.preventDefault();
var category = '<?php echo strtolower($category); ?>';
// var id = '<?php echo $blogger_id; ?>';
// var firstname = '<?php echo $firstname; ?>';
// var surname = '<?php echo $surname; ?>';
// alert(posts + category + id + firstname + surname);
$.ajax({
type: "GET",
url: "http://curtains.dev/wp-admin/admin-ajax.php",
dataType: 'html',
data: ({
action: 'get_new',
id: 46
// firstname: firstname,
// surname: surname,
// posts: posts,
// category: category
}),
success: function(data){
$('.card-product__img--cat').hide('fast');
$('.card-products-cat-wrap').hide().fadeIn('slow').html(data);
$('.card-products-cat-wrap').append('<div className="clearfix"></div>');
}
});
});
});
</script>