Forked from azizultex/Ajax loading image gallery WordPress
Created
March 1, 2018 06:36
-
-
Save slavapas/b16b3d8e1c337d7b20b09227347a7619 to your computer and use it in GitHub Desktop.
Ajax loading image gallery WordPress ( coded for oliveorange.com )
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
/* localize script */ | |
wp_enqueue_script( 'settings', get_template_directory_uri() . '/js/settings.js', array('jquery'), OO_Version, true ); | |
wp_localize_script( 'settings', 'localized', array( | |
'ajaxurl' => admin_url( 'admin-ajax.php' ) | |
)); | |
/* setting.js */ | |
jQuery(function($){ | |
var counter = 0; | |
function loadGallery() { | |
$.ajax({ | |
type: "POST", | |
url: localized.ajaxurl, | |
data: ({ action: 'ajax_gallery', counter: counter }), | |
beforeSend: function(){ | |
$('.gallery').append('<p class="loading">loading...</p>'); | |
}, | |
success: function(msg){ | |
$('.gallery').append(msg).hide().fadeIn('slow'); | |
$('.gallery a').simpleLightbox(); | |
$('.gallery').find('.loading').fadeOut('slow'); | |
} | |
}); | |
counter++; | |
} | |
loadGallery(); | |
$('.load-btn a').click(function( e ){ | |
e.preventDefault(); | |
loadGallery(); | |
}); //event handler | |
}); | |
/* shortcode */ | |
function gallery_query_shortcode($atts) { | |
$output_string = '<div class="gallery"></div>'; | |
$output_string .= '<div class="load-btn"> | |
<a href="#">Load more</a> | |
</div>'; | |
return $output_string; | |
} | |
add_shortcode('gallery', 'gallery_query_shortcode'); | |
/* ajax response */ | |
function query_gallery_func($atts) { | |
/* get gallery items from admin option by piklist */ | |
$oo_options = get_option('oliveorange_setting'); | |
$gallery = $oo_options['gallery']; | |
$counter = $_POST['counter']; | |
$organized_arr = array_chunk($gallery, 8); | |
/* get number of clicks from ajax */ | |
$current_loop = $organized_arr[$_POST['counter']]; | |
if(isset($current_loop)) : | |
ob_start(); | |
?> | |
<?php foreach($current_loop as $k => $v) : ?> | |
<a href="<?php echo wp_get_attachment_image_src($v, 'full')[0]; ?>"><img src="<?php echo wp_get_attachment_image_src($v, 'gallery')[0]; ?>" alt="Event Image" title="Event Image" /> | |
</a> | |
<?php endforeach; ?> | |
<?php else : ?> | |
<p> No more item found! </p> | |
<?php endif; ?> | |
<?php | |
$output_string = ob_get_contents(); | |
ob_end_clean(); | |
echo $output_string; | |
exit(); | |
} | |
add_action('wp_ajax_ajax_gallery','query_gallery_func'); | |
add_action('wp_ajax_nopriv_ajax_gallery','query_gallery_func'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment