字preSS博客分页返回404页

字preSS博客分页返回404页

本文介绍了字preSS博客分页返回404页,$后没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装在旧网站的博客,并想尽一切办法让分页工作。

I've setup a blog on an older site and have tried everything to get the pagination to work.

下面是从品类blog.php的文件中的code:

Here is the code from the category-blog.php file:

    <section>
    <div id="content">
        <h1 class="blog-heading">In The News</h1>
            <?php
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

            get_posts(array(
                'post_type'      => 'post', // You can add a custom post type if you like
                'paged'          => $paged,
                'posts_per_page' => 4
            ));
            ?>
        <?php if( have_posts() ): ?>

            <div id="blog-grid">

                <?php while (have_posts()) : the_post(); ?>
                <div class="blog-post" id="blog-post-<?php get_the_ID(); ?>">
                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'blog-index' ); ?></a>
                    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                    <?php the_excerpt(); ?>
                    <div class="readmore-link">
                        <a href="<?php the_permalink(); ?>">Read More</a>
                    </div><!-- end div.readmore-link -->
                </div><!-- end div.blog-post -->
                <?php endwhile; ?>
            </div><!-- end div#blog-grid -->

            <div class="pagination">
                <?php my_pagination(); ?>
            </div><!-- /.navigation -->

        <?php endif; ?>

    </div><!-- end content -->
</section>

和functions.php文件

and the functions.php file

    if ( ! function_exists( 'my_pagination' ) ) :
    function my_pagination() {
        global $wp_query;

        $big = 999999999; // need an unlikely integer
        $paginate_links = paginate_links( array(
            //'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages,
            'prev_next' => False
        ) );
        if ( $paginate_links ) {
            echo $paginate_links;
        }

    }
endif;

当我进入博客页面/博客,我能看到帖子的上市..如预期。分页呈现以下职位(同样,如预期),但是当你浏览到,比方说,第2页..你去/博客/页/ 2 /,并得到一个404错误,指出$后没有定义!

When I access the blog page at /blog I can see the listing of posts.. as expected. The pagination renders below the posts (again, as expected) but when you navigate to, say, page 2.. you go to /blog/page/2/ and get a 404 error stating $post is not defined!

我试图克服这种与每一个岗位,我可以在网上找的,从宣布posts_per_page和pre_get_posts在这样的functions.php的:

I've tried to overcome this with every post I could find online, from declaring posts_per_page and pre_get_posts in the functions.php like:

define('PER_PAGE_DEFAULT', 4);
function my_post_count_queries( $query ) {
  if (!is_admin() && $query->is_main_query()){
       $query->set('posts_per_page', 4);
  }
}
add_action( 'pre_get_posts', 'my_post_count_queries' );

add_action( 'pre_get_posts', 'modify_blog_query' );
function modify_blog_query( $query ) {
    if ( !is_admin() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', 4 );
    }
}


function change_posts_per_page(){ return 1; }
if( preg_match("|\/".get_option('tag_base')."\/.+\/page\/[0-9]+$|i", $_SERVER['REQUEST_URI']) ){
    add_filter( 'pre_option_posts_per_page' , 'change_posts_per_page');
}

甚至要远修改的.htaccess重写规则忽略/博客网页..我连装了几个插件,它呈现分页作为我的功能呢..,仍然造成了404。

and even going to far as modify the .htaccess rewrite rules to ignore the /blog pages.. I even installed a couple plugins, which rendered the pagination as my function does.. and still resulted in a 404.

我也换成标准preV /下一步整个分页功能,具有相同的结果:

I've also replaced the entire pagination function with standard Prev/Next, with the same results:

                    <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
                <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

最后,因为这是现有的,正在运行的网站。我不能禁用任何现有的插件,看是否一个或更多的是引起冲突或错误。

Lastly, since this is an existing, live site.. I cannot disable any existing plugins to see if one or more are causing a conflict or error.

任何帮助和最好的回馈/更好的方式来做到这一点,是极大的AP preciated。

Any help and feedback on the best/a better way to do this, is greatly appreciated.

更新时间:

我的分页链接取代了get_posts查询与自定义查询。基于弥敦道的建议如下(感谢的!)......不过,同样的404。

I replaced the get_posts query with a custom query.. based on Nathan's recommendation below (thanks for that!).. still, same 404 on pagination links.

下面是我使用的自定义查询:

Here is the custom query I used:

        <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $temp = $wp_query;
        $wp_query = null;
        $wp_query_args = array(
                            'post_type'=> 'post',
                            'posts_per_page' => 4,
                            'paged'=> $paged
                        );
        $wp_query = new WP_Query( $wp_query_args );
        if ( $wp_query->have_posts() ) : ?>
        <div id="blog-grid">
        <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

        <div class="blog-post" id="blog-post-<?php get_the_ID(); ?>">
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'blog-index' ); ?></a>
            <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
            <?php the_excerpt(); ?>
            <div class="readmore-link">
                <a href="<?php the_permalink(); ?>">Read More</a>
            </div><!-- end div.readmore-link -->
        </div><!-- end div.blog-post -->

        <?php endwhile; ?>
        </div><!-- end div#blog-grid -->
        <?php endif; ?>

        <div class="pagination">
            <?php my_pagination(); ?>
        </div><!-- /.navigation -->

        <?php
          $wp_query = null;
          $wp_query = $temp;  // Reset
        ?>

第二次修订

这页是活的,所以我提供了一个链接,那些愿意提供帮助,来测试:

This page is live, so I'm providing a link for those willing to help, to test with:

博客首页

博客第2页,错误页

推荐答案

一吨经过研究,摆弄和编码。我意识到,分页功能并没有加入该类别基地的联系 - 例如。 /分类/博客/只有返回/沼泽地/ ......奇怪的是,在/博客页面的工作......但是,分页时,事实并非如此。

After a ton of research, fiddling and coding.. I realized that the pagination function was not adding the category base to the link - eg. /category/blog/ and only returning /bog/ ... oddly enough, the /blog page worked... but, when paging, it didn't.

所以,我创建了一个新的模板,并更名为类别。我安装WP_PageNavi和使用的分页。这解决了这个问题。所以,这个分页功能的Floating左右,即有很多人似乎可以用,不能正常工作。

So, I created a new template and renamed the category. I installed WP_PageNavi and used that for pagination. That resolved the issue.. so, this pagination function that's floating around, that a lot of people seem to be using, is not working properly.

也就是说,这一个:

    if ( ! function_exists( 'my_pagination' ) ) :
    function my_pagination() {
        global $wp_query;

        $big = 999999999; // need an unlikely integer
         if( get_option('permalink_structure') ) {
             $format = 'category/page/%#%';
         } else {
             $format = 'category/?paged=%#%';
         }

        $paginate_links = paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            //'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
            'format' => $format,
            'current' => max( 1, get_query_var('page') ),
            'total' => $wp_query->max_num_pages,
            'prev_next' => False
        ) );
        if ( $paginate_links ) {
            echo $paginate_links;
        }

    }
endif;

这不会prePEND类的基本路径..这将导致分页失败的重定向。

It will not prepend the category base path.. which causes the pagination to fail the redirect.

这篇关于字preSS博客分页返回404页,$后没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 00:35