Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

wordpress - ACF Data does not show when the template part is loaded via Ajax

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...