Wordpress加载下一篇文章使用AJAX无法使用自定义帖子类型

时间:2014-05-28 13:40:29

标签: php jquery wordpress custom-post-type

我正在使用此plugin加载我的下一篇文章,使用AJAX。我已将其实施到我的网站中,它显示了更多帖子按钮,但是一旦点击“加载更多帖子”按钮,它就会显示没有任何内容可以加载。

以下是我的代码:

    // 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)
        )
    );
}

我正在尝试在自定义帖子上执行此操作,我认为问题可能在于它所说的is_singular。我尝试将此更新为以下内容:

if( !is_post_type_archive($work) )

但是,当我将其更改为此时,它不再检测到插件并返回标准分页。

以下是jQuery:

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;
});
});

2 个答案:

答案 0 :(得分:0)

您可以使用自定义解决方案解决此问题,但可能无法对插件进行编程。

您可以做的是制作自定义的帖子类型单页模板 这将获取$ _post值,如果为true,则显示条带化版本 仅改为内容。

然后在你想要这个ajax加载下一页功能的页面上 您可以使用一些wordpress函数和jquery来触发包含帖子值的下一个帖子链接,然后jquery将替换页面当前Content Div中的内容。

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

jquery将调用它并显示它

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

答案 1 :(得分:0)

问题不在于PHP,但在jQuery中它正在寻找错误的类。我已经改变了课程.work现在它的作用就像我的帖子那样。

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;
});});