php参数为arg的自定义查询分页问题

时间:2019-05-25 19:55:37

标签: wordpress loops pagination customization

我已经使用自定义查询创建了一个循环。此查询包含一个PHP参数,我通过GET方法从网址获取。这是我的循环页面中的代码:

    $cat = get_queried_object();
    echo '<h1 class="childcatdes">'. $cat->name . '</h1>';
    echo '<p class="childcatdescr">'. $cat->description . '<br><br></p>';
    do_action( 'woocommerce_before_single_product' );

    $posts_per_page = 12;
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;


    $product_args = array(
        'post_type' => 'product',
        'posts_per_page' => $posts_per_page,
        'paged' => $paged,
        'page' => $paged,
        'tax_query' => array(
             'relation' => 'AND',
            array(
              'taxonomy' => 'product_cat',
              'field'    => 'name',
              'terms'    => $cat->name,
            ),
            array(
              'taxonomy' => 'manufacturers',
              'field'    => 'slug',
              'terms'    => $_GET['filter_manufacturers'],
              'operator' => 'IN'
            )
        ),
        'orderby' => 'name',
        'order' => 'ASC',
    );

    $custom_query = new WP_Query( $product_args );

    if($custom_query->have_posts()) {
        echo '<ul';
        while ($custom_query->have_posts() )  : $custom_query->the_post(); 
            echo '<li>';
                $link = get_the_permalink();

                echo '<a href="' . $link . '">' . get_the_title() . '</a>';

            echo '</li>';
        endwhile;


        echo '</ul>';
    }
    else {
        echo 'No post found.';
    }
    ?>
    <nav class="pagination">
        <?php pagination_bar( $custom_query); ?>
    </nav>

并且我将此代码用于函数pagination_bar的我的functions.php文件中

function pagination_bar( $custom_query) {
    $total_pages = $custom_query->max_num_pages;
    $big = 999999999; // need an unlikely integer
    if ($total_pages > 1){
        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => preg_replace('/\?.*/', '/', get_pagenum_link(1)) . '%_%',
           'current' => $current_page,
           'format' => 'page/%#%/',
           'total' => $custom_query->max_num_pages,
           'add_args' => array(
               'filter_manufacturers' => $_GET['filter_manufacturers'],
           )
        ));
    }
}

问题是分页(即使它算出首页上正确的帖子)也无法正常工作。我在第二页上收到404错误。

0 个答案:

没有答案