Skip to content

Instantly share code, notes, and snippets.

@phucdohong96
Created November 4, 2017 05:03
Show Gist options
  • Save phucdohong96/b173fea5dc9e83a2242c98725d759772 to your computer and use it in GitHub Desktop.
Save phucdohong96/b173fea5dc9e83a2242c98725d759772 to your computer and use it in GitHub Desktop.
Loop with custom pagination ajax
<?php
function pager_loop_function() {
ob_start();
$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
$args1 = array(
'post_type' => 'post',
'paged' => $paged1,
'posts_per_page' => 6,
);
echo '<div id="event1">';
$query1 = new WP_Query( $args1 );
$count = 0;
while ( $query1->have_posts() ) : $query1->the_post();
$id = get_the_ID();
$title = get_the_title();
$image = get_the_post_thumbnail($id,'large');
$excerpt = get_the_excerpt();
$link = get_permalink ($id);
$content = get_the_content();
$author = get_the_author();
?>
<div>HTMl</div>
<?php
$count++;
endwhile;
echo '<div class="clearfix"></div>';
/*---------------------- Pagination 1 ------------------------*/
$pag_args1 = array(
'format' => '?paged1=%#%',
'current' => $paged1,
'total' => $query1->max_num_pages,
'add_args' => array( 'paged2' => $paged2 )
);
$pager1 = paginate_links( $pag_args1 );
echo '<div class="shortcode_pager">'.$pager1.'</div>';
/*---------------------- End ------------------------*/
echo '</div>';
//End Div Ajax
?>
<!-- Script Pagination Ajax -->
<script type="text/javascript">
jQuery(function($) {
$('#event1').on('click', '.shortcode_pager a', function(e){
e.preventDefault();
var link = $(this).attr('href');
$('#event1').fadeOut(100, function(){
$(this).load(link + ' #event1', function() {
$(this).fadeIn(100);
});
});
});
});
</script>
<?php
wp_reset_postdata();
return ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment