-
-
Save DavidPeralvarez/566e493a0d5c78e12b4f84575d59d89e to your computer and use it in GitHub Desktop.
Cargar más con Ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Load more | |
add_action( 'wp_ajax_tt_load_more', 'tt_load_more' ); | |
add_action( 'wp_ajax_nopriv_tt_load_more', 'tt_load_more' ); | |
function tt_load_more(){ | |
check_ajax_referer( 'tt_nonce' ); | |
// create the query | |
$args = json_decode( $_POST['queryVars'], true ); | |
$args['paged'] = $_POST['currentPage'] + 1; | |
$args['post_status'] = 'publish'; | |
query_posts( $args ); | |
if ( have_posts() ) : | |
while ( have_posts() ) : | |
the_post(); | |
get_template_part( 'content', get_post_format() ); | |
endwhile; | |
endif; | |
wp_die(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Al inicio | |
global $wp_query; | |
// Después del loop de WordPress | |
if( $wp_query->max_num_pages > 1 ): | |
echo '<div class="twentytwelve-loadmore">' . esc_html__( 'Load more', 'twentytwelve' ) . '</div>'; | |
endif; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Load more Ajax */ | |
.twentytwelve-loadmore { | |
padding: 10px; | |
border: 2px solid #21759b; | |
color: #21759b; | |
border-radius: 4px; | |
display: block; | |
text-align: center; | |
cursor: pointer; | |
font-size: 18px; | |
font-weight: bold; | |
} | |
.twentytwelve-loadmore:hover { | |
background-color: #21759b; | |
color: white; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$( ".twentytwelve-loadmore" ).on( "click", function() { | |
var loadMoreBtn = $(this); | |
$.ajax({ | |
type: 'post', | |
dataType: 'html', | |
url: tt_parameters.ajax_url, | |
data: { | |
action: 'tt_load_more', | |
_ajax_nonce: tt_parameters.nonce, | |
queryVars: tt_parameters.query_vars, | |
currentPage: tt_parameters.current_page, | |
}, | |
beforeSend: function(){ | |
console.log( 'Cargando...' ); | |
}, | |
success: function( response ){ | |
if( response ) { | |
loadMoreBtn.prev().after( response ); | |
tt_parameters.current_page++; | |
if( tt_parameters.current_page == tt_parameters.max_page ) { | |
loadMoreBtn.remove(); | |
} | |
} else { | |
loadMoreBtn.remove(); | |
} | |
} | |
}); | |
}); | |
})( jQuery ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment