I am trying to create a list/grid view toggle on a page on Wordpress site and I have set up the Ajax call on click of view button, but for some reason only the HTML markup is loading, none of the Advanced Custom Field data is populating within the HTML.
<div class="text-lg w-full view-toggles">
<a id="list-view-button" name="view-button" data-value="list">List View</a>
<a id="grid-view-button" name="view-button" data-value="grid">Grid View</a>
</div>
function add_this_script_footer(){ ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$( 'a[name=view-button]' ).on('click', function() {
var activeView = $(this).data('value');
console.log(activeView);
$.ajax({
type: 'GET',
url: '<?php echo admin_url('admin-ajax.php');?>',
data: {
'action':'toggle_view',
'activeView' : activeView
},
success:function(data) {
$("#view-container").html(data);
},
error: function(errorThrown){
window.alert(errorThrown);
}
});
});
});
</script>
<?php }
add_action('wp_footer', 'add_this_script_footer');
add_action( 'wp_ajax_toggle_view', 'toggle_view' );
add_action( 'wp_ajax_nopriv_toggle_view', 'toggle_view' );
function toggle_view() {
if ( isset($_REQUEST) ) {
$view = $_REQUEST['activeView'];
if ( $view == 'list' ) {
get_template_part('partials/view', 'list');
} elseif ( $view == 'grid' ) {
get_template_part('partials/view', 'grid');
}
}
die();
}
question from:
https://stackoverflow.com/questions/65541134/acf-data-does-not-show-when-the-template-part-is-loaded-via-ajax 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…