【问题标题】:WP_Query on function not showing paginationWP_Query 函数不显示分页
【发布时间】:2018-04-11 17:39:33
【问题描述】:

我有这段代码,它工作正常,唯一的问题是它没有显示分页。此代码在我的 functions.php 文件中。

add_action( 'wp_ajax_nopriv_load-filter', 'prefix_load_cat_posts' );
add_action( 'wp_ajax_load-filter', 'prefix_load_cat_posts' );

function prefix_load_cat_posts () {
    global $post;
    $cat_id = $_POST[ 'cat' ];
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array (
        'cat' => $cat_id,
        'posts_per_page' => 6,
        'order' => 'DESC',
        'paged' => $paged
    );
    $cat_query = new WP_Query($args);
    if($cat_query->have_posts()) :
        while($cat_query->have_posts()) : $cat_query->the_post();
            get_template_part( 'template-parts/content', get_post_format() );
        endwhile;
        wp_reset_query();
?>
    <div class="page-nation">
        <ul class="pagination pagination-large">
        <?php 
            $pagination = get_the_posts_pagination(array(
                'mid_size'  => 2,
                'prev_text' =>esc_html__('Previous', 'travel-tour'),
                'next_text' => esc_html__('Next', 'travel-tour'),
                'screen_reader_text' => ' ',
            ) );
            echo $pagination;
        ?>
        </ul>
    </div> 
<?php
    endif;
    die(); 
}

这是我的 jquery ajax 脚本

<script>

jQuery(document).ready(function () {
 jQuery('.js-category-button').on('click', function(e){
  e.preventDefault();
  jQuery('.preloadswitch').addClass('addpreloader');
  var catID = jQuery(this).data('slug');
  var catName =  jQuery(this).attr('href');
  var ajaxurl = '<?php echo esc_js( admin_url( 'admin-ajax.php' ) ) ?>';
    jQuery.ajax({
        type: 'POST',
        url: ajaxurl,
        crossDomain : true,
        dataType: 'html',
        data: {"action": "load-filter", cat: catID },
        beforeSend: function () {
            jQuery(".the-categories").html('<div></div>').fadeIn('slow');
            jQuery(".page-nation").hide();
            //window.location.hash = "#"+jQuery("#comehere").attr("id");
            jQuery('html,body').animate({scrollTop:jQuery('#comehere').offset().top}, 1000);
            window.history.pushState('obj', 'newtitle', catName);
        },
        success: function(response) {
            jQuery(".the-categories").append(response);
            jQuery('.preloadswitch').removeClass('addpreloader');
            return false;
        }
    });
})

}); 

</script>

我不再在这里做什么了,我一直在网上搜索可能的工作功能,但没有运气。

我只需要显示分页

【问题讨论】:

    标签: ajax wordpress pagination


    【解决方案1】:

    注释掉或删除wp_reset_query();,或移至分页后;并使用paginate_links() 而不是get_the_posts_pagination()。

    这是 prefix_load_cat_posts() 函数中更改后的“if-else”块:

        if($cat_query->have_posts()) :
            while($cat_query->have_posts()) : $cat_query->the_post();
                get_template_part( 'template-parts/content', get_post_format() );
            endwhile;
            //wp_reset_query();
    ?>
        <div class="page-nation">
            <ul class="pagination pagination-large">
            <?php 
                $pagination = paginate_links(array(
                    'mid_size'  => 2,
                    'prev_text' =>esc_html__('Previous', 'travel-tour'),
                    'next_text' => esc_html__('Next', 'travel-tour'),
                    'current' => $paged,
                    'total' => $cat_query->max_num_pages,
                    'type' => 'array',
                    'base' => home_url( '/%_%' ),
                ) );
                echo '<li>' . implode( '</li><li>', $pagination ) . '</li>';
            ?>
            </ul>
        </div> 
    <?php
        endif;
    

    [编辑] 尝试设置base 参数。

    【讨论】:

    • 嗨@Sally CJ,我已将其更改为 paginate_links() 并且它可以工作,但现在问题是分页链接及其链接像这样mywebsite.com/wp-admin/admin-ajax.php/page/3
    • 你好,它现在显示分页链接,但它没有重定向到正确的页面,喜欢来自类别,所以每次点击链接时路径都会改变。
    • 好的,我可以帮你解决这个问题;但请分享 HTML 标记。而且,你的问题已经回答了,对吧?我的意思是,这个问题:“我只需要显示分页”。
    • 不要介意 HTML 标记。请参阅我的评论here。
    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    相关资源
    最近更新 更多