Javascript ajax中的分页无法转到正确的页面

Javascript ajax中的分页无法转到正确的页面,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我正在使用ajax根据过滤器加载帖子。如果超过9个,我将使用分页。虽然分页调用正确的页数,但链接本身不起作用。相反,它们会转到一个以url+/wp admin/admin ajax.php?paged=2结尾的页面。该页是空白的,上面有一个0。就这样 当我使用javascript动态地将分页链接覆盖到正确的链接中时,页面只会转到相同的原始页面 感谢您的帮助 myfunctions.php中的ajax代码 add_action( 'wp_ajax_myfilter', 'posts_filter_

我正在使用ajax根据过滤器加载帖子。如果超过9个,我将使用分页。虽然分页调用正确的页数,但链接本身不起作用。相反,它们会转到一个以url+/wp admin/admin ajax.php?paged=2结尾的页面。该页是空白的,上面有一个0。就这样

当我使用javascript动态地将分页链接覆盖到正确的链接中时,页面只会转到相同的原始页面

感谢您的帮助

myfunctions.php中的ajax代码

add_action( 'wp_ajax_myfilter', 'posts_filter_function' );
add_action( 'wp_ajax_nopriv_myfilter', 'posts_filter_function' );

function posts_filter_function() {

$args = array(
    'order'          => 'DESC',
    'post_type'      => 'post',
    'post_status'    => 'publish',
    'posts_per_page' => 9,
    'paged' => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1,
);

// for categories!
if( isset( $_POST['categoryfilter'] ) )
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'field'    => 'id',
            'terms'    => $_POST['categoryfilter']
        )
    );

// for topics!
// if( isset( $_POST['topicfilter'] ) )
// $args['tax_query'] = array(
//  array(
//      'taxonomy' => 'topics',
//      'field' => 'id',
//      'terms' => $_POST['topicfilter']
//  )
// );

$query = new WP_Query( $args );
$backup_image = get_field( 'featured_image_global', 'option' );
?>
<div class="news-grid grid-x small-up-1 medium-up-3">
    <?php
    if ( $query->have_posts() ) :
        while ( $query->have_posts() ) : $query->the_post();
            $image = get_the_post_thumbnail_url( $post->ID, 'featured-large' );
            if ( $image ) {
                $image = get_the_post_thumbnail_url( $post->ID, 'featured-large' );
            } else {
                $image = $backup_image;
            }
            ?>
            <div class="cell posts__filter--item">
                <a href="<?php echo esc_html( get_post_permalink( $post->ID ) ); ?>">
                    <div class="card">
                        <div class="card-section">
                        <img
                        src="<?php echo esc_attr( $image ); ?>"
                        alt="<?php echo esc_html( get_post_meta( get_post_thumbnail_id( $post->ID ), '_wp_attachment_image_alt', true ) ); ?>">

                            <p class='date'><?php echo esc_html( get_the_date( 'd M \'y', $post->ID ) ); ?></p>
                            <h4><?php echo esc_html( get_the_title( $post->ID ) ); ?></h4>
                            <p class='author'><?php echo esc_html( post_authors( $post ) ); ?></p>
                        </div>
                    </div>
                </a>
            </div>
            <?php
        endwhile;
        ?>
    </div>
            <div class="pagination--container">
        <?php
        echo paginate_links(
            array(
                'base'      => get_home_url() . '/news/' . '%_%',
                'format'    => '?paged=%#%',
                'current'   => max( 1, get_query_var( 'paged' ) ),
                'total'     => $query->max_num_pages,
                'prev_text' => '1',
                'next_text' => '1',
            )
        );
        ?>
    </div>
        <?php
        wp_reset_postdata();
    else :
        echo '<h2 class="no-posts">No posts found</h2>';
    endif;
    die();
}

问题在于
'base'
属性,该属性将从呈现它的页面引用。如WordPress文档中所述,如果使用
str_replace($big、%#%')、esc_url(get_pagenum_link($big))
,它将从当前页面创建一个url,该url在
admin ajax.php
文件中将变为
/wp admin/admin ajax.php

因此,我们需要使
base
属性动态化

动态输入字段

在表单中添加另一个隐藏的输入元素,该元素引用您所在的页面。这样,我们就不必更改JS,如果需要,可以在HTML中添加或删除输入字段

<?php
global $wp;
$base = home_url( $wp->request ); // Gets the current page we are on.
?>

<input type="hidden" name="base" value="<?php echo $base; ?>"/>
然后将基值作为参数传递给
paginate\u links
函数

// Fallback if there is not base set.
$fallback_base = str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) );

// Set the base.
$base = isset( $_POST[ 'base' ] ) ? trailingslashit( $_POST[ 'base' ] ) . '%_%' : $fallback_base;
paginate_links(
    array(
        'base'      => $base,
        'format'    => '?paged=%#%',
        'current'   => max( 1, get_query_var( 'paged' ) ),
        'total'     => $query->max_num_pages,
        'prev_text' => '1',
        'next_text' => '1',
    )
);
因此,输入字段的值将决定
base
属性将变成什么

备选方案:也可以简单地将当前URL添加到jQuery Ajax请求中。在
数据
属性中传递序列化数据之前,添加:

'&base=' + location.hostname + location.pathname;

这比输入解决方案更不可取,因为它需要某种硬编码,但在无法操作HTML时可能是一种选择。

您是否尝试过更改
分页链接
函数中的
'base'
属性?嘿,Emiel。是的,我有。我尝试了
'base'=>获取pagenum\u链接(1)。“%\unum%”好吧,我已经遇到过你的情况好几次了,我也用JS手动更改了链接。但是现在我看到它,我认为
'base'
属性是罪魁祸首。请尝试使用分页页面的url对基本属性进行硬编码,并附加
'%\%'
,以了解我的意思。我很感谢您在这里查看此属性的可能副本。我试着做了你刚才提到的事情,这给了我经典的
404页面未找到
paginate_links(
    array(
        'base'      => $base,
        'format'    => '?paged=%#%',
        'current'   => max( 1, get_query_var( 'paged' ) ),
        'total'     => $query->max_num_pages,
        'prev_text' => '1',
        'next_text' => '1',
    )
);
'&base=' + location.hostname + location.pathname;