admin管理员组文章数量:1296246
I am using Modula Plugin to speedup the galleries creation process. It works wonderfully but I don't understand how to render it correctly on the ajax call. I read through the available posts on rendering shortcodes via ajax, but so far I struggle to implement it in my code. Here is the code I use:
add_action('wp_ajax_nopriv_getHistory', 'getHistory_ajax');
add_action('wp_ajax_getHistory', 'getHistory_ajax');
function getHistory_ajax(){
$args = array(
'post_type' => 'gallery',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'post_gallery_gallery_year',
'value' => $festivalQueryYear,
'compare' => 'LIKE'
)
),
'meta_key' => 'post_gallery_gallery_year',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$gallery= new WP_Query($args);
if($gallery->have_posts()) : while($gallery->have_posts()): $gallery->the_post();
$shortcode = get_field('gallery_shortcode');
echo do_shortcode($shortcode);
endwhile; wp_reset_postdata(); endif;
}
in ajax.js
$(".festival-year-toggler-js-ajax").on("click", function (e) {
$.ajax({
url: wpAjax.ajaxUrl,
data: { action: "getHistory", festivalQueryYear },
type: "POST",
success: function (data) {
$('.target-div').html(data);
},
error: function (error) {
console.warn(error);
},
});
})
The result is, the images from the gallery are rendered but without the specified layout, obviously, because the shortcode is not running through its default environment. How can I run this shortcode so that it renders on the ajax call in the same way as on normal page loading?
Thank you so much for your help!!!
I am using Modula Plugin to speedup the galleries creation process. It works wonderfully but I don't understand how to render it correctly on the ajax call. I read through the available posts on rendering shortcodes via ajax, but so far I struggle to implement it in my code. Here is the code I use:
add_action('wp_ajax_nopriv_getHistory', 'getHistory_ajax');
add_action('wp_ajax_getHistory', 'getHistory_ajax');
function getHistory_ajax(){
$args = array(
'post_type' => 'gallery',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'post_gallery_gallery_year',
'value' => $festivalQueryYear,
'compare' => 'LIKE'
)
),
'meta_key' => 'post_gallery_gallery_year',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$gallery= new WP_Query($args);
if($gallery->have_posts()) : while($gallery->have_posts()): $gallery->the_post();
$shortcode = get_field('gallery_shortcode');
echo do_shortcode($shortcode);
endwhile; wp_reset_postdata(); endif;
}
in ajax.js
$(".festival-year-toggler-js-ajax").on("click", function (e) {
$.ajax({
url: wpAjax.ajaxUrl,
data: { action: "getHistory", festivalQueryYear },
type: "POST",
success: function (data) {
$('.target-div').html(data);
},
error: function (error) {
console.warn(error);
},
});
})
The result is, the images from the gallery are rendered but without the specified layout, obviously, because the shortcode is not running through its default environment. How can I run this shortcode so that it renders on the ajax call in the same way as on normal page loading?
Thank you so much for your help!!!
Share Improve this question asked Mar 28, 2021 at 12:39 iuriiGaviuriiGav 335 bronze badges2 Answers
Reset to default 1I can only comment on this on a general level as I'm not familiar with the plugin you're using. (This might also not be the best place to ask this kind of plugin related questions as 3rd party plugins are currently considered off-topic here on WPSE.)
Majority of this kind of plugins I've encountered have some kind of javascript (or jQuery), which is executed on page load, which setup the layout, enable lightboxes and other features. After the scripts have run, they are done and don't care about what happens to the DOM or html afterwards. This means that, if you add new html to the page via Ajax, you need to find a way to fire those same setup scripts again yourself.
$.ajax({
success: function (data) {
// add html to the DOM
$('.target-div').html(data);
// the basic idea..
some_vendor_function_to_init_gallery(
$('.target-div')
);
},
});
If these setup scripts are globally available depends on the plugin you're using. You need to either dig into the plugin's source code, browse its documentation, and/or contact the plugin author. If the setup script is not in the global scope, then you're probably out of luck.
This is the snippet I got from Modula team to reinitialize the plugin:
var modulaGalleries = jQuery('.modula.modula-gallery');
jQuery.each(modulaGalleries, function () {
var modulaID = jQuery(this).attr('id'),
modulaSettings = jQuery(this).data('config');
jQuery('#' + modulaID).modulaGallery(modulaSettings);
});
As Antti wrote, it needs to be added like this:
$.ajax({
success: function (data) {
// add html to the DOM
$('.target-div').html(data);
var modulaGalleries = jQuery('.modula.modula-gallery');
jQuery.each(modulaGalleries, function () {
var modulaID = jQuery(this).attr('id'),
modulaSettings = jQuery(this).data('config');
jQuery('#' + modulaID).modulaGallery(modulaSettings);
});
});
本文标签: Render Modula Plugin Shortcode On Ajax Request
版权声明:本文标题:Render Modula Plugin Shortcode On Ajax Request 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741635073a2389594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论