-
-
Save the-cc-dev/6724e978c9974c0422f46dcc31798118 to your computer and use it in GitHub Desktop.
class for create ajax load more button in wordpress
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 | |
class Loadmore_Button { | |
public $article_wrapper = 'article-card'; | |
public $post_per_load = 6; | |
public $button_text = 'Показать еще'; | |
public $button_loading_text = 'Загрузка...'; | |
public $button_data_attr = 'data-aos="fade-up"'; | |
public $orderby = 'date'; | |
public $article_class = '.all-news__item'; | |
public $button_class = 'all-news__btn-more'; | |
public function _construct($args) { | |
//take arguments from attributes | |
if (in_array('article_wrapper', $args)) | |
$this->article_wrapper = $args['article_wrapper']; | |
if (in_array('post_per_load', $args)) | |
$this->post_per_load = $args['post_per_load']; | |
if (in_array('button_text', $args)) | |
$this->button_text = $args['button_text']; | |
if (in_array('button_loading_text', $args)) | |
$this->button_loading_text = $args['button_loading_text']; | |
if (in_array('button_data_attr', $args)) | |
$this->button_data_attr = $args['button_data_attr']; | |
if (in_array('article_class', $args)) | |
$this->article_class = $args['article_class']; | |
if (in_array('button_class', $args)) | |
$this->button_class = $args['button_class']; | |
//add actions wp | |
add_action( 'wp_enqueue_scripts', array($this, 'enqueue_script')); | |
add_action('wp_ajax_loadmore', array($this, 'ajax_handler')); | |
add_action('wp_ajax_nopriv_loadmore', array($this, 'ajax_handler')); | |
//init button | |
$this->button_init(); | |
} | |
private function get_template_name($wrapper) { | |
$wrapper = str_split($this->article_wrapper, strripos($this->article_wrapper, '-')); | |
$wrapper[1] = str_replace('-', '', $wrapper[1]); | |
return $wrapper; | |
} | |
private function enqueue_script() { | |
global $wp_query; | |
wp_register_script( 'loadmore', get_stylesheet_directory_uri() . '/js/loadmore.js', array('jquery') ); | |
wp_localize_script( 'loadmore', 'loadmore_params', array( | |
'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX | |
'posts' => json_encode( $wp_query->query_vars ), // посты | |
'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 2, | |
'max_page' => $wp_query->max_num_pages, | |
'all_posts' => $wp_query->found_posts, | |
'category' => $wp_query->cat, | |
'btn_text' => $this->button_text, | |
'btn_load_text' => $this->button_loading_text, | |
'article_class' => $this->article_class, | |
)); | |
wp_enqueue_script( 'loadmore' ); | |
} | |
private function ajax_handler() { | |
// prepare our arguments for the query | |
$args = json_decode( stripslashes( $_POST['query'] ), true ); | |
$wrapper = $this->get_template_name($this->article_wrapper); | |
$args['paged'] = $_POST['page'] + 1; // we need next page to be loaded | |
$args['posts_per_page'] = $this->post_per_load; | |
$args['orderby'] = $this->orderby; | |
$args['offset'] = isset($_POST['offset']); | |
query_posts( $args ); | |
if( have_posts() ) : | |
while( have_posts() ): the_post(); ?> | |
<?php get_template_part( $wrapper[0], $wrapper[1] ); ?> | |
<?php | |
endwhile; | |
endif; | |
die; | |
} | |
public function button_init() { | |
global $wp_query; | |
if ( $wp_query->max_num_pages > 1 ) { | |
echo '<div class="loadmore-wrapper"> | |
<button class="'. $this->button_class .' btn-more" ' . $this->button_data_attr. ' | |
title="'.$this->button_text .'"> | |
' . $this->button_text . ' | |
</button> | |
</div>'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment