Support

Account

Home Forums General Issues get specific values from repeater fields with ajax Reply To: get specific values from repeater fields with ajax

  • Hey John,

    the “try and error” method actually works. My idea was to create json array for all my extra contents and get the specific values out of it. Im surprised it works ๐Ÿ™‚

    Can you please tell me if this is the proper way doing this or if there could be performance issues with this method?

    function in functions.php:

    function get_extra_content() { 
    $content = array();
    $args = array(
    	'post_type' => array('page')
    );
    $result = new WP_Query($args);
    if ($result->have_posts()): while ($result->have_posts()) : $result->the_post();
    	if (have_rows('sections')) : while ( have_rows('sections') ) : the_row();
    		if( have_rows('textbild')) : while ( have_rows('textbild') ) : the_row();
    			if( have_rows('inhalte') ) : while ( have_rows('inhalte') ) : the_row();					
    				if( have_rows('inhalt') ) : while ( have_rows('inhalt') ) : the_row();
    					$more = get_sub_field('weitere_inhalte');
    					$content[] = array(
    						'content' => $more,
    					);									
    				endwhile; endif;			
    			endwhile; endif;
    		endwhile; endif;	
    	endwhile; endif;
    endwhile; endif;
    echo json_encode($content);
    wp_die();
    }

    the ajax stuff:

    $('.morebtn').click(function() {
    		sectionID = $(this).data('id');
    		if ($(this).hasClass('open')) {
    			$('#section'+sectionID+' .content .more .inner').empty();
    			$(this).stop().toggleClass('open');
    		}
    		else {
            	show_read_more(sectionID);
            	$(this).stop().toggleClass('open');
            }
    	});
    
    	function show_read_more(sectionID) {
    
    		$.ajax({
    			url: ajax_actions.ajaxurl, 
    			dataType: 'json',  
    			data: {
    				action: "get_extra_content"
    			},
    			beforeSend: function(){
    				$('.spinner').show();	
    			},
    			complete: function(){
    				$('.spinner').hide();		
    			},
    			success: function (data) {
    				//console.log(data[sectionID].content);
    				$('#section'+sectionID+' .content .more .inner').append(data[sectionID].content);
                },
                error: function (errorThrown) {
                    $('#section'+sectionID+' .content .more .inner').append("error");
                }
    		});
    	}