0

Please bear with me because I am new to utilizing Wordpress PHP this way. I have the following script:

<div id="content">
<?php
    // check if the repeater field has rows of data
    if( have_rows('repeat_field') ):
        $i = 0;

        // loop through the rows of data
        while ( have_rows('repeat_field') ) : the_row();
            $i++;

            // display a sub field value
            if (!empty(get_sub_field('feature_image_post'))) {

                echo '<a href="';
                the_sub_field('restaurant_article_link'); 
                echo '">';
            } 
            if( $i >= 78 ) {
                break;
            }            
        endwhile;
    else :
        // no rows found
    endif;
    ?>
    </div>
    <div id="loading-image" style="display: none;"></div>
    <div id="part2"></div>
    <script>

        var w = $(window);
        window.onscroll = function(ev) {
            if ($(document).height() - w.height() == w.scrollTop()) {

                $('#loading-image').show();
                $.ajax({
                    url: "<?php echo get_stylesheet_directory_uri();?>/post-2.php",
                    cache: false,
                    success: function(result){
                      $("#part2").html(result);
                    },
                    complete: function(){
                      $('#loading-image').hide();
                    }
                });
            }
        };
    </script>

The the Php page that I am trying to load pretty much contains the same code . It just stops after loading 200 fields.

<?php
    // check if the repeater field has rows of data
    if( have_rows('repeat_field') ):
        $i = 0;
        // loop through the rows of data
        while ( have_rows('repeat_field') ) : the_row();
            $i++;
            // display a sub field value
            if (!empty(get_sub_field('feature_image_post'))) {
                echo '<a href="';
                the_sub_field('restaurant_article_link'); 
                echo '">';
            } 
            if( $i >= 200 ){
              break;
            }            

        endwhile;
    else :
        // no rows found
    endif;
?>

I don't know why the php page (post-2.php) is not loading. The strange thing is that when I replace <?php echo get_stylesheet_directory_uri();?>/post-2.php with a test html page <?php echo get_stylesheet_directory_uri();?>/test.html The test page (test.html) loads. I have to say that I looked on in my console using the console tab of my browser added console.log(); to view any errors and I'm not getting any errors. I also looked in my networked tag to ensure that post-2.php loaded properly and I'm not getting errors. My goal is to find out why my PHP page isn't loading

***** Update ***** I'm being told to use $post_id but I've never used it. Does anyone have a working example of this? Would it be similar to if( have_rows('repeat_field', $post_id) ): or if( have_rows('repeat_field', 55612) ): ?

  • Put some thing in else part so that you could get some result when you have no rows to return. – Bikash Waiba Jun 19 '17 at 9:39
1

I have not used ACF before but i suspect your post-2.php lacks $post_id as in have_rows($field_name, $post_id)(see). If you are using ajax there is no way that your ajax function know the page you are on and what post you are seeking so you better use $post_id to tell ajax function which post data you want.

Correct way of using ajax in wordpress is directing ajax request to ajaxurl = admin_url().'admin-ajax.php'. It is better to wp_localize_script but can do like this <div id="part2" data-url="<?php echo admin_url().'admin-ajax.php'; ?>"></div> and the ajaxurl = $('#part2').data('url);

You can use something like this

 $.ajax({
                url: ajaxurl,
                cache: false,

                type : 'post',
                data : {
                   action: 'load_post_acf' // This will be used in hook
                 },

                success: function(result){
                   $("#part2").html(result);
                   $('#loading-image').hide();
                },

           });

Ajax function for post-2.php you can do something like ..

add_action('wp_ajax_nopriv_load_post_acf','your_ajax_function');
add_action('wp_ajax_load_post_acf','your_ajax_function');

function your_ajax_function(){
  global $post;
  if( have_rows('repeat_field',$post->ID) ):
    // do something
  else:
   echo '<h4>No rows found.</h4>'; //do something else
  endif;


}
  • Thanks! Very helpful. So what is admin-ajax.php? The URL Page that I am going to is named post-2.php Are you saying that in my template page my ajax call should be similar to the following ajaxurl = admin_url().'admin-ajax.php' or ajaxurl = admin_url().'post-2.php'? – Mariton Jun 19 '17 at 10:35
  • ajax-admin.php is the core file of wordpress do some research a ajaxurl should be admin_url().'admin-ajax.php' and your post-2.php should be included in themes functions.php file. – Bikash Waiba Jun 19 '17 at 10:54
  • @ Bikash Waiba I tried your code example and it isn't working for me. I understand that Post Id should work but when I implemented your code example it didn't work for me $post_id – Mariton Jun 19 '17 at 14:52
  • There is no global $post_id, you need to pass post ID with data, the same way you pass action. – Milo Jun 19 '17 at 15:51
  • 1
    global $post; and $post_id = $post->ID; without that you have no $post_id variable. – mrben522 Jun 19 '17 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.