Last active
June 15, 2022 20:09
-
-
Save lucasprogamer/b32ca6210381c41d485de0e6729a406c to your computer and use it in GitHub Desktop.
Load More wp
This file contains hidden or 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 | |
#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' ); ?> |
This file contains hidden or 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 | |
if (!class_exists('Load_More_Posts')): | |
/** | |
* Load More | |
* Uses WP pagination | |
*/ | |
class Load_More_Posts | |
{ | |
/** | |
* hooks | |
*/ | |
public function __construct() | |
{ | |
add_action('wp_enqueue_scripts', array( | |
$this, | |
'enqueue_assets' | |
)); | |
add_action('wp_ajax_load_more_posts', array( | |
$this, | |
'load_more_posts' | |
)); | |
add_action('wp_ajax_nopriv_load_more_posts', array( | |
$this, | |
'load_more_posts' | |
)); | |
} | |
/** | |
* Assets | |
*/ | |
public function enqueue_assets() | |
{ | |
wp_enqueue_script('load-more-posts-js', get_template_directory_uri(). '/assets/js/load-more.js', array( | |
'jquery' | |
) , '0.1', true); | |
wp_localize_script('load-more-posts-js', 'wp_ajax_url', admin_url('admin-ajax.php')); | |
} | |
/** | |
* | |
* @return void | |
*/ | |
public function load_more_button($context, $text, $template, $paged) | |
{ | |
global $wp_query; | |
// Lets recreate the current query within our ajax call | |
wp_localize_script('load-more-posts-js', 'load_more_data', array( | |
'query' => $wp_query->query | |
)); | |
echo '<div id="load-more-posts-area-wrapper" class="grid clearfix"></div>'; | |
wp_nonce_field('load-more-posts-nonce-' . $context, 'load-more-posts-nonce'); | |
echo '<div class="textAligCenter clearfix">'; | |
echo '<div id="load-more-posts-error" class="load-more-posts-error error" style="display:none;">' . esc_html__('Sem mais posts.', 'load-more-posts') . '</div>'; | |
echo '<button id="load-more-posts" class="link_botao" data-template="' . esc_attr__($template, 'load-more-posts') . '" data-context="' . esc_attr__($context, 'load-more-posts') . '" data-paged="' . esc_attr__($paged, 'load-more-posts') . '" data-max-pages="' . $wp_query->max_num_pages . '">' . esc_html__($text, 'load-more-posts') . '</button>'; | |
echo '</div>'; | |
} | |
/** | |
* Ajax posts | |
*/ | |
public function load_more_posts() | |
{ | |
if (empty($_POST['nonce']) || empty($_POST['paged']) || !wp_verify_nonce($_POST['nonce'], 'load-more-posts-nonce-' . $_POST['context'])) { | |
exit; | |
} | |
else { | |
global $post; // required by setup post data | |
$context = (!empty($_POST['context'])) ? sanitize_text_field($_POST['context']) : 'default'; | |
$template = (!empty($_POST['template'])) ? sanitize_text_field($_POST['template']) : 'default'; | |
$args = (array)$_POST['query']; | |
$args['paged'] = sanitize_text_field($_POST['paged']); | |
// A filter for query | |
$args = apply_filters('load-more-posts-args-' . sanitize_text_field($_POST['context']) , $args); | |
$query = new WP_Query($args); | |
$posts = $query->get_posts(); | |
foreach($posts as $post) { | |
setup_postdata($post); | |
get_template_part($template); | |
wp_reset_postdata(); | |
} | |
} | |
exit; | |
} | |
} | |
new Load_More_Posts(); | |
endif; |
This file contains hidden or 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
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