Answer the question
In order to leave comments, you need to log in
When loading images via ajax, lightbox does not work, how to fix it?
Lightbox only works for the first loaded images. After using ajax in uploaded images, opening by clicking on them does not work, the lightbox does not open, you can only scroll starting from the first images that were opened initially.
How to fix? Tell me please.
function get_materials() {
global $post;
$materials_slugs = $_POST['materials_slugs'] ? $_POST['materials_slugs'] : '';
$paged = $_POST['paged'] ? $_POST['paged'] : 1;
$terms_slugs = explode(',',$materials_slugs);
$return_html = '';
$args = array(
'post_type' => 'attachment',
'paged' => $paged,
'post_mime_type' => 'image',
'post_status' => 'any',
'orderby' => 'title',
' order' => 'ASC',
'tax_query' =>
'terms' => $terms_slugs,
),
),
);
$materials = new WP_Query($args);
$max_pages = $materials->max_num_pages;
if ($materials->have_posts()):
while ($materials->have_posts()): $materials->the_post();
$title = get_the_title();
$return_html .='';
$image_full = wp_get_attachment_image_src( $post->ID, 'full' );
$image_thumb = wp_get_attachment_image_src( $post->ID, 'thumbnail' );
$return_html .= ' ';
$return_html .=' ';
$return_html .= '' . $title. '';
endwhile;
endif;
if ($paged < $max_pages) {
$next_page = $paged + 1;
$return_html .= ' Show more ';
}
wp_reset_postdata();
echo $return_html;
wp_die();
}
add_action('wp_ajax_get_materials', 'get_materials');
add_action('wp_ajax_nopriv_get_materials', 'get_materials');
Answer the question
In order to leave comments, you need to log in
This is because these elements are created dynamically (they were not originally on the page). Therefore, after loading posts, you need to update the lightbox - read the documentation for the plugin
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question