简体   繁体   中英

Wordpress AJAX load page content

I'm trying to get wordpress to load my other pages through AJAX, first i thought i wouldn't append other pages HTML but it seems as if the page doesn't know how to find the page ID.

My jQuery file ajax-pagination.js

(function($) {
function find_page_number( element ) {
    element.find('span').remove();
    return parseInt( element.html() );
}
$(document).on( 'click', '.menu-item a', function( event ) {
    event.preventDefault();

    page = find_page_number( $(this).clone() );
    $.ajax({
        url: ajaxpagination.ajaxurl,
        type: 'post',
        data: {
            // action: 'ajax_pagination',
            action: 'my_ajax_pagination',
            query_vars: ajaxpagination.query_vars,
            page: page
        },
    beforeSend: function() {
        $('#main').find( 'article' ).remove();
    $('#nav-burgermenu-btn').removeClass('active');
    $('#nav').removeClass('active');
        $(document).scrollTop();
        $('#main').append( '<div class="page-content" id="loader">Loading New Posts...</div>' );
    },
    success: function( html ) {
        $('#main #loader').remove();
        $('#main').append( html );
    }
    })
})
})(jQuery);

My Wordpress Functions PHP file

Everything is loaded in like this:

global $wp_query;
wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'query_vars' => json_encode( $wp_query->query )
));

And then this code follows:

add_action( 'wp_ajax_nopriv_ajax_pagination', 'my_ajax_pagination' );
add_action( 'wp_ajax_ajax_pagination', 'my_ajax_pagination' );

function my_ajax_pagination() {
    $query_vars = json_decode( stripslashes( $_POST['query_vars'] ), true );

    $query_vars['paged'] = $_POST['page'];


    $posts = new WP_Query( $query_vars );
    $GLOBALS['wp_query'] = $posts;

    add_filter( 'editor_max_image_size', 'my_image_size_override' );

    if( ! $posts->have_posts() ) {
        get_template_part( 'content', 'none' );
    }
    else {
        while ( $posts->have_posts() ) {
            $posts->the_post();
            get_template_part( 'content', get_post_format() );
        }
    }
    remove_filter( 'editor_max_image_size', 'my_image_size_override' );

    the_posts_pagination( array(
        'prev_text'          => __( 'Previous page', 'twentyfifteen' ),
        'next_text'          => __( 'Next page', 'twentyfifteen' ),
        'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
    ) );

    die();
}

function my_image_size_override() {
    return array( 825, 510 );
}

Right now it appends a 0 to the HTML when a link in my nav is clicked. It's like something is going wrong in the find_page_number function since the $('#main').append( html ); won't append html.. but i'm not sure.

The problem is with in this part:

 else {
    while ( $posts->have_posts() ) {
        $posts->the_post();
        get_template_part( 'content', get_post_format() );
    }

I replaced the get_post_format() with 'page' and it worked.

else {
    while ( $posts->have_posts() ) {
        $posts->the_post();
        get_template_part( 'content', 'page' );
    }

Also make sure that 'content' is actually 'content' and not something else like 'template-parts/page/content'.

(original code is from https://premium.wpmudev.org/blog/load-posts-ajax/ )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM