• Hi,
    I have a simple script that load a gallery via ajax on click.
    Here’s a code:
    FUNCTIONS.PHP

    add_action('wp_ajax_load_gallery_callback', 'load_gallery_callback');
    add_action('wp_ajax_nopriv_load_gallery_callback', 'load_gallery_callback');
    
    function load_gallery_callback(){
    	//require_once('../../../wp-load.php');
    	$args = array('post_type'=>'mapa','');
    	$name = $_POST['name'];
    	$query = new WP_Query($args);
    	$break = false;
    
    	if ($query->have_posts()): while($query->have_posts()): $query->the_post();
    	if (get_the_title() == $name){
    		$zdjecia = get_field('zdjęcia');
    		echo apply_filters( 'post_gallery', $zdjecia );//do_shortcode('[gallery link="file" ids="5,6,7,8,9,10"]');
    		$break = true;
    		break;
    	}
    	endwhile;
    	endif;
    
    	if (!$break) echo 'Brak zdjęć';
    	die();
    }

    MAIN.JS

    function get_zdjecia($name){
    	$.ajax({
    		type: "POST",
    		url: ajax_object.ajaxurl,//"<?php //echo bloginfo('wpurl').'/ajax.php?post_id=1'; ?>",
    		data: {
    			action : "load_gallery_callback",
    			name : $name
    		},
    		success: function(data){
    			console.log(data);
    		}
    	}).done(function(ret){
    		$('#zdjecia').html(ret);
    	}).fail(function(ret){
    		console.log('błąd');
    		$('#zdjecia').html('Brak zdjęć');
    	});
    	$('html, body').animate({
            scrollTop: $("#scroll").offset().top
        }, 2000);
    };

    And button:
    <p class="wiecej" onclick="get_zdjecia('<?php echo $nazwa;?>')\" >Zobacz zdjęcia</p>

    When I click on button then correct images from right gallery show up. BUT generated gallery has set link to attachment file instead media file. Of course I set in admin panel link to media file before.

    What is wrong? How to fix it? I need to have gallery with link to media file.

    Regards,

  • The topic ‘do_shortcode gallery via ajax’ is closed to new replies.