Infinite scroll within Ajax

So, I have Ajax enabled on my WordPress site as the following:

id within any anchor within the chosen div will be saved and used to find an appropriate php file then loaded at an appropriate content div.

Read More

PHP

<!--Menu part -->
 <div class="royal_front_menu">
      <a href="#rcp_front_front_id" class="mdl-tabs__tab is-active" id="front_id">All</a>       
      <a href="#rcp_front_electronic_id" class="mdl-tabs__tab" id="electronic_id">Electronics</a>         
</div>

<!--Content Part-->
<div class="active" id="rcp_front_front_id">  
     <div class="spinner"></div>
</div>
<div class="not_active" style="display:none;" id="rcp_front_electronic_id"> 
     <div class="spinner"></div> 
</div>      


<script>
//Ajax loading
jQuery(document).ready(function() {
    jQuery('.royal_front_menu a').click(function(e) {
        e.preventDefault();

        var tab_id = jQuery(this).attr('id'); 

        jQuery.ajax({
            type: "GET",
            url: "<?php echo admin_url('admin-ajax.php'); ?>", 
            dataType: 'html',
            data: ({ action: 'royal_front_tab', id: tab_id}),
            success: function(data){
                  jQuery('#rcp_front_' + tab_id).html(data);


        },
        error: function(data)  
        {  
        alert("Error!");
        return false;
        }  

        }); 

 }); 
 });
</script>

FUNCTION.PHP

//Ajax call for front page for Mobile
function royal_front_tab_rhm_callback() {  
     $template_part_path = 'page-parts/rhm/front/01_front_menu_' .  $_GET['id'];
get_template_part($template_part_path);
exit;
}
 add_action('wp_ajax_royal_front_tab_rhm', 'royal_front_tab_rhm_callback');
 add_action('wp_ajax_nopriv_royal_front_tab_rhm', 'royal_front_tab_rhm_callback');

01_front_menu_front_id.php

<div class="content">
    <?php echo do_shortcode('[Post_Shortcode]'); ?> 
</div>

So far so good…

So, with the following setup, everything is working fine. All the contents are called only when clicked etc. GREAT!

However, I am trying to add one more feature: Infinite scroll

What I have done so far:

I followed the following instruction: http://wptheming.com/2012/03/infinite-scroll-to-wordpress-theme/

So, I tried this on a separate dummy site to see how it works before messing things up.

SET UP

Add the following to the function.php:

// To add infinitescroll js
function custom_theme_js(){
    wp_register_script( 'infinite_scroll',  get_template_directory_uri() . '/custom_js/jquery.infinitescroll.min.js', array('jquery'),null,false );
    wp_enqueue_script('infinite_scroll');

}
add_action('wp_enqueue_scripts', 'custom_theme_js');

//To add selectors
function custom_infinite_scroll_js() {
    { ?>
    <script>
    jQuery('.royal_card.site-row-fluid.site-grid-list').infinitescroll({
        navSelector  : jQuery('.site-pagination'),
        nextSelector : jQuery('.site-pagination > a.next'),
        itemSelector :  '.royal_card.site-row-fluid.site-grid-list > .royal_card_outer.royal_card_content_grid',
        contentSelector: jQuery('.royal_card.site-row-fluid.site-grid-list'),
    msgText: "Yay"  
    },function(newElements){
        jQuery('.site-separator').remove();
        jQuery('.royal_card.site-row-fluid.site-grid-list > .royal_card_outer.royal_card_content_grid').after('');
     });
    </script>
    <?php
     }
 }
add_action( 'wp_footer', 'custom_infinite_scroll_js',100 );

When I tried this on a dummy site (without Ajax), it works fine. I can get the next page content as infinite scroll.

The "Normal" URL from the dummy site is this:

 <a class="page-numbers" href="http://example.com/page/2/">2</a>

Problem

So, with the Ajax page, I found something peculiar. The page URL became somewhat odd:

 <a class="page-numbers" href="http://example.com/wp-admin/admin-ajax.phppage/2/?action=royal_front_tab&amp;id=front_all_id">2</a>

Because it is not a normal URL, it leads to a “404” page as it does not exist.

Here is my guess

I thought that jQuery('.royal_front_menu a').click(function(e) { which any anchor within royal_front_menu div will be affected. But the content is loaded outside of the div.

Okay, I am confused to why it is happening.

Any suggestions?

Related posts

1 comment

  1. Seems to me like it doesn’t add a / after admin-ajax.php, if you try going to the link below does it give you a valid page then?

    <a class="page-numbers" href="http://example.com/wp-admin/admin-ajax.php/page/2/?action=royal_front_tab&amp;id=front_all_id">2</a>
    

Comments are closed.