Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Load More wp
#Usage
add in functions
function load_more_button($context = 'default', $text = 'Carregar Mais', $template = 'parts/cards', $paged = 0)
{
if (empty($paged)) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
}
$load_more = new Load_More_Posts();
$load_more->load_more_button($context, $text, $template, $paged);
}
include('functions/load-more-posts.php');
query on template
<?php
$args = array(
'posts_per_page' => 3,
'post_type' => 'post',
);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part('parts/cards');
endwhile;
endif;
load_more_button('', 'Carregar Mais', 'parts/cards' ); ?>
jQuery( document ).ready( function( $ ) {
// JS to submit ajax for load more button
$(document).on('click', '#load-more-posts', function(e) {
$( '#load-more-posts-error' ).hide();
var previouspaged = $( this ).attr( 'data-paged' );
var currentpaged = parseInt( previouspaged )+ 1;
jQuery.ajax({
type: 'POST',
url: wp_ajax_url,
data: {
action : 'load_more_posts',
paged: currentpaged,
context: $(this).attr( 'data-context' ),
template: $(this).attr( 'data-template' ),
query: load_more_data.query,
nonce: $( '#load-more-posts-nonce' ).val(),
},
dataType: 'html'
})
.done( function( response ) {
if ( response ) {
$( '#load-more-posts-area-wrapper' ).append( response );
if ( currentpaged > parseInt( $( '#load-more-posts' ).attr( 'data-max-pages' ) ) ) {
$( '#load-more-posts' ).hide();
} else {
$( '#load-more-posts' ).attr( 'data-paged', currentpaged );
}
} else {
$( '#load-more-posts' ).hide();
$( '#load-more-posts-error' ).show();
}
} );
e.preventDefault();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment