Skip to content

Instantly share code, notes, and snippets.

@aryanrajseo
Last active April 26, 2023 18:15
Show Gist options
  • Save aryanrajseo/083d54fd129c8505c6c09adc5073de1f to your computer and use it in GitHub Desktop.
Save aryanrajseo/083d54fd129c8505c6c09adc5073de1f to your computer and use it in GitHub Desktop.
gpt loadmore
function load_more_scripts() {
wp_enqueue_script( 'loadmore', get_stylesheet_directory_uri() . '/js/loadmore.js', array('jquery'), '1.0', true );
wp_localize_script( 'loadmore', 'loadmore_params', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'posts' => json_encode( $args ),
'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
'max_page' => $query->max_num_pages
) );
}
add_action( 'wp_enqueue_scripts', 'load_more_scripts' );
function load_more_posts() {
$query = json_decode( stripslashes( $_POST['query'] ), true );
$page = $_POST['page'];
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 2,
'paged' => $page,
);
$args = wp_parse_args( $query, $args );
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
// Display the post title and content
echo '<h2>' . get_the_title() . '</h2>';
// echo '<div>' . get_the_content() . '</div>';
endwhile;
endif;
wp_reset_postdata();
wp_die();
}
add_action( 'wp_ajax_load_more_posts', 'load_more_posts' );
add_action( 'wp_ajax_nopriv_load_more_posts', 'load_more_posts' );
function latest_posts_shortcode() {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 2,
'paged' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
// Display the post title and content
echo '<h3>' . get_the_title() . '</h3>';
endwhile;
endif;
wp_reset_postdata();
// Add the load more button
$max_pages = $query->max_num_pages;
if ( $max_pages > 1 ) {
echo '<button id="load-more" data-page="2">Load More</button>';
}
//wp_die();
}
add_shortcode( 'latest_posts', 'latest_posts_shortcode' );
// /js/loadmore.js
jQuery(function($){
$(document).on('click', '#load-more', function(e){
e.preventDefault();
var button = $(this),
page = button.data('page'),
data = {
'action': 'load_more_posts',
'page': page,
'query': loadmore_params.posts
};
$.ajax({
url : loadmore_params.ajaxurl,
data : data,
type : 'POST',
beforeSend : function ( xhr ) {
button.text('Loading...');
},
success : function( data ){
if( data ) {
button.text( 'Load More' ).prev().before(data);
button.data('page', page+1);
if ( page+1 == loadmore_params.max_page )
button.remove();
} else {
button.remove();
}
}
});
});
});
function mytheme_get_latest_posts( $post_count = 2, $offset = 0 ) {
global $post;
//global $latest_posts;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $post_count,
'offset' => $offset,
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => get_option( 'sticky_posts' ), // do not display the sticky posts at all.
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
while ( $latest_posts->have_posts() ) {
$latest_posts->the_post();
$title = get_the_title();
echo '<h2>'.$title.'</h2>';
}
} else {
echo '<p>No posts found.</p>';
}
wp_reset_postdata();
}
function mytheme_enqueue_scripts() {
wp_enqueue_script( 'mytheme-ajax', get_stylesheet_directory_uri() . '/js/mytheme-ajax.js', array( 'jquery' ), '1.0.0', true );
wp_localize_script( 'mytheme-ajax', 'mytheme_ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_scripts' );
add_shortcode('mytheme_ajax_posts', 'mytheme_display_posts_and_load_more_button');
function mytheme_display_posts_and_load_more_button() {
ob_start();
echo '<div id="latest-posts">';
mytheme_get_latest_posts();
echo '</div>';
echo '<button id="load-more-posts" data-offset="2">Load More Posts</button>';
$html = ob_get_clean();
return $html;
}
add_action( 'wp_ajax_mytheme_load_more_posts', 'mytheme_ajax_load_more_posts' );
add_action( 'wp_ajax_nopriv_mytheme_load_more_posts', 'mytheme_ajax_load_more_posts' );
function mytheme_ajax_load_more_posts() {
$offset = isset( $_POST['offset'] ) ? intval( $_POST['offset'] ) : 0;
mytheme_get_latest_posts( 2, $offset );
wp_die();
}
jQuery(document).ready(function ($) {
if (typeof mytheme_ajax !== 'undefined') {
$("#load-more-posts").click(function () {
let button = $(this);
let offset = button.data("offset");
$.ajax({
type: "POST",
url: mytheme_ajax.ajax_url,
data: {
action: "mytheme_load_more_posts",
offset: offset,
},
beforeSend: function () {
button.text("Loading...");
},
success: function (response) {
button.text("Load More Posts");
$("#latest-posts").append(response);
button.data("offset", offset + 2);
},
error: function () {
button.text("Load More Posts");
alert("Error loading more posts.");
},
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment