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");
}
});
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
๐ ACF & ACF PRO 6.0.7 are now available.
— Advanced Custom Fields (@wp_acf) January 18, 2023
โจThis release contains bug fixes and improvements while we continue to work on the next major release of ACF.https://t.co/wQgAOpwmUI
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.