“如何修复PHP中的“自定义分页404错误”

时间:2019-08-01 18:31:56

标签: wordpress

我在自定义分页中遇到问题。自定义分页可以在具有相同代码的单页上正常工作,但不能在另一页上工作。

我已经多次更新了永久链接,但没有任何反应。我还更新了.htaccess文件。

我的源代码在这里列出...

<div id="home" class="tab-pane fade in active">
                        <p class="main_dv"><?php
                            //$count=1;
                            $paged = (get_query_var('page')) ? get_query_var('page') : 1;
                                $data= new WP_Query(array(
                                'post_type'=>'software',// your post type name
                                'category_name' => 'general_software', // your category name
                                'posts_per_page' => 5, // post per page
                                'paged' => $paged,));
                                if($data->have_posts()) :
                                while($data->have_posts())  : $data->the_post();?>
                            <div class="testcustompost1">
                                <div class="pst_div">
                                <div class="thumb">
                                    <?php the_post_thumbnail();?> 
                                </div>
                                <div class="title_des">
                                <div class="title">
                                    <?php the_title();?>
                                </div>
                                <div class="description">
                                    <?php the_content();?>  
                                </div>
                                <div class="downlod_btn">
                                    <button class="btn"><a href="<?php echo the_permalink(); ?>" target="_blank"> Download </a></button>
                                </div>
                                </div>
                                </div>
                            </div>
                            <?php //$count++;
                            endwhile;

                            echo '<div class="paginat_design">';
                            $total_pages = $data->max_num_pages;
                            if ($total_pages > 1){
                            $current_page = max(1, get_query_var('paged'));
                            echo paginate_links(array(
                                        'base' => get_pagenum_link(1) . '%_%',
                                        'format' => '/page/%#%',
                                        'current' => $current_page,
                                        'total' => $total_pages,
                                        'prev_text'    => __('« prev'),
                                        'next_text'    => __('next »'),
                                    ));
                                }
                            echo '</div>'?>
                                <?php else :?>
                                <?php _e('404 Error&#58; Not Found', ''); ?>
                                <?php endif; ?>
                                <?php wp_reset_postdata();?> 
                        </p>

                    </div>

404找不到错误

1 个答案:

答案 0 :(得分:0)

您应该尝试以下操作:

<?php
        /**
         * Looping through the content
         */
        $page = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
        $data = new WP_Query (array(
                        'post_type'=>'software',
                        'category_name' => 'general_software', 
                        'posts_per_page' => 5,
                        'page' => $page
                    )); ?>
        <?php while ($data -> have_posts()) : $data -> the_post(); ?>
            <!-- Your html code here -->
        <?php endwhile; ?><?php wp_reset_query(); ?><?php wp_reset_postdata(); 
?>

这是分页部分:

<?php
            echo paginate_links( array(
                'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
                'total'        => $data->max_num_pages,
                'current'      => max( 1, get_query_var( 'page' ) ),
                'format'       => '?page=%#%',
                'show_all'     => false,
                'type'         => 'plain',
                'end_size'     => 2,
                'mid_size'     => 1,
                'prev_next'    => true,
                'prev_text'    => sprintf( '<i></i> %1$s', __( 'Previous Page', 'text-domain' ) ),
                'next_text'    => sprintf( '%1$s <i></i>', __( 'Next Page', 'text-domain' ) ),
                'add_args'     => false,
                'add_fragment' => '',
            ) );
?>

让我知道这是否对您有帮助。如果不是,请尝试将值从paged更改为page