Wordpress Cargar las siguientes publicaciones con AJAX que no funciona con el tipo de publicación personalizada

Estoy usando esto plugin to load in my next posts with AJAX. I have implemented it into my site and it is showing the load more posts button however once you click on the 'load more posts' button it then says there is nothing to load.

A continuación se muestra mi código:

    // Add code to index pages.
if( !is_singular() ) {  
    // Queue JS and CSS
    wp_enqueue_script(
        'pbd-alp-load-posts',
        plugin_dir_url( __FILE__ ) . 'js/load-posts.js',
        array('jquery'),
        '1.0',
        true
    );

    wp_enqueue_style(
        'pbd-alp-style',
        plugin_dir_url( __FILE__ ) . 'css/style.css',
        false,
        '1.0',
        'all'
    );



    // What page are we on? And what is the pages limit?
    $max = $wp_query->max_num_pages;
    $paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;

    // Add some parameters for the JS.
    wp_localize_script(
        'pbd-alp-load-posts',
        'pbd_alp',
        array(
            'startPage' => $paged,
            'maxPages' => $max,
            'nextLink' => next_posts($max, false)
        )
    );
}

I am trying to do this on a custom post and I think the problem may be where it says is_singular. I tried updating this to the following:

if( !is_post_type_archive($work) )

But when i change it to this it not longer detects the plugin and goes back to the standard pagination.

Here is the jQuery as well:

jQuery(document).ready(function($) {

// The number of the next page to load (/page/x/).
var pageNum = parseInt(pbd_alp.startPage) + 1;

// The maximum number of pages the current query can return.
var max = parseInt(pbd_alp.maxPages);

// The link of the next page of posts.
var nextLink = pbd_alp.nextLink;

/**
 * Replace the traditional navigation with our own,
 * but only if there is at least one page of new posts to load.
 */
if(pageNum <= max) {
    // Insert the "More Posts" link.
    $('#postContent')
        .append('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>')
        .append('<p id="pbd-alp-load-posts"><a href="#">Load More Posts</a></p>');

    // Remove the traditional navigation.
    $('.navigation').remove();
}


/**
 * Load new posts when the link is clicked.
 */
$('#pbd-alp-load-posts a').click(function() {

    // Are there more posts to load?
    if(pageNum <= max) {

        // Show that we're working.
        $(this).text('Loading posts...');

        $('.pbd-alp-placeholder-'+ pageNum).load(nextLink + ' .post',
            function() {
                // Update page number and nextLink.
                pageNum++;
                nextLink = nextLink.replace(/\/page\/[0-9]?/, '/page/'+ pageNum);

                // Add a new placeholder, for when user clicks again.
                $('#pbd-alp-load-posts')
                    .before('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>')

                // Update the button message.
                if(pageNum <= max) {
                    $('#pbd-alp-load-posts a').text('Load More Posts');
                } else {
                    $('#pbd-alp-load-posts a').text('No more posts to load.');
                }
            }
        );
    } else {
        $('#pbd-alp-load-posts a').append('.');
    }   

    return false;
});
});

preguntado el 28 de mayo de 14 a las 14:05

2 Respuestas

You can just use a custom solution to this issue, the plugin might not be programmed to do this.

what you can do is make a custom post type single page template that will take a $_post value and if true then display a striped out version of the contents only instead.

and then on the page that you want this ajax load next page function you can use some wordpress functions plus jquery to just trigger a next post link with post values and then jquery will replace the contents in the current Content Div of your page.

if(isset($_POST["location"]))
    { the_content(); } else { normal page ..

the jquery will call it and display it

        $(".next").click(function(){
            $( ".result" ).show();
            $.post( pathnamehere, {
                postnamehere:"True"
            }).done(function( data ) {
                $( ".result" ).html( data );
            });
        });

contestado el 28 de mayo de 14 a las 20:05

The problem with this wasnt with the PHP but in the jQuery it was looking for the wrong class. I have changed the class .work and it now works as thats the class my posts get.

jQuery(document).ready(function($) {

// The number of the next page to load (/page/x/).
var pageNum = parseInt(pbd_alp.startPage) + 1;

// The maximum number of pages the current query can return.
var max = parseInt(pbd_alp.maxPages);

// The link of the next page of posts.
var nextLink = pbd_alp.nextLink;

/**
 * Replace the traditional navigation with our own,
 * but only if there is at least one page of new posts to load.
 */
if(pageNum <= max) {
    // Insert the "More Posts" link.
    $('#postContent')
        .append('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>')
        .append('<p id="pbd-alp-load-posts"><a href="#">Load More Posts</a></p>');

    // Remove the traditional navigation.
    $('.navigation').remove();
}


/**
 * Load new posts when the link is clicked.
 */
$('#pbd-alp-load-posts a').click(function() {

    // Are there more posts to load?
    if(pageNum <= max) {

        // Show that we're working.
        $(this).text('Loading posts...');

        $('.pbd-alp-placeholder-'+ pageNum).load(nextLink + ' .work',
            function() {
                // Update page number and nextLink.
                pageNum++;
                nextLink = nextLink.replace(/\/page\/[0-9]?/, '/page/'+ pageNum);

                // Add a new placeholder, for when user clicks again.
                $('#pbd-alp-load-posts')
                    .before('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>')

                // Update the button message.
                if(pageNum <= max) {
                    $('#pbd-alp-load-posts a').text('Load More Posts');
                } else {
                    $('#pbd-alp-load-posts a').text('No more posts to load.');
                }
            }
        );
    } else {
        $('#pbd-alp-load-posts a').append('.');
    }   

    return false;
});});

contestado el 28 de mayo de 14 a las 21:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.