如何在Ajax结果中添加数字分页,该结果显示按自定义分类法过滤的播客(自定义帖子)

时间:2019-02-21 11:36:46

标签: ajax wordpress

我想在此页面上添加数字分页(并动态加载帖子),称为“ page-podcast”,在这里我通过Ajax调用过滤我的podcast(自定义帖子),并使用这些标记,体裁,国家/地区并输入:https://imgur.com/Qw8JOlC

在我的前端page-podcast.php中,我有这个div,其中出现了帖子:

<div id="datafetch">Search results will appear here</div>

在页面function.php中,我添加了调用Ajax的函数

add_action( 'wp_footer', 'ajax_fetch' );
function ajax_fetch() {
?>
<script type="text/javascript">
    function fetch(e){
    jQuery.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: { action: 'data_fetch', keyword: keywords},
        success: function(data) {
            jQuery('#datafetch').html( data );
        }
    });

}

</script>

以下代码是ajax函数(始终在function.php上):

add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){

    ...filter part, omitted, because it has nothing to do with the question...

   $the_query = new WP_Query($query_args); 

    if( $the_query->have_posts() ) :
        while( $the_query->have_posts() ): $the_query->the_post(); ?>

            <h2><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h2>

        <?php endwhile;
        else: ?>
        <p class="no-criteria"><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php
        wp_reset_postdata();  
    endif;

    die();
}

有人要分享一些提示/示例/代码吗?因为我尝试了很多在网络上找到的解决方案,但是没有用。

1 个答案:

答案 0 :(得分:0)

已解决。 因此,在循环结束之前,我已经在ajax函数中添加了页数:

\documentclass{beamer}

\usetheme{Warsaw}
\setbeamercolor{background canvas}{bg=black}

\begin{document}

\begin{frame}
    \titlepage
\end{frame} 

\end{document}

$ numberPages等于:

<?php if ($numberPages > 1): ?>
<ul>
    <?php
    for ($i = 1; $i <= $numberPages; $i++) {
        echo '<li><a name="page" href="#" class="pagination" value="' . $i . '">'
            . $i . '</a></li>';
    }
    ?>
</ul>

我创建一个全局变量,称为$ page,设置为0。 然后我参与了两个活动:

$the_query = new WP_Query($query_args);
$numberPages = $the_query->max_num_pages;

“。premuto”是一个元素,其中包含我要传递给ajax(自定义分类法)的ID,并且页面设置为1。

“。pagination”是帖子的页码列表(1,2,...)。

最后,在我的ajax函数上,我得到了页码:

   $(document).on("click touchend", ".premuto", function (event) {
        event.preventDefault();
        page = 1;
        keywords['page'] = page;
        keywords[$(this).attr('name')] = $(this).attr('value');
        fetch(this);
    });
    $(document).on("click touchend", ".pagination", function (event) {
        event.preventDefault();
        keywords[$(this).attr('name')] = $(this).attr('value');
        fetch(this);
    });

在我的wp_query中添加:

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