Correction of article pagination link after WordPress setting pseudo static

WordPress one hundred and forty-five 10.9K Reading mode

We usually set the WordPress fixed link to/% postname%. html or/% post_id%. html, which can make the page look like a static page, but when the article has pagination, the pagination link will become strange, such as:

/morning-paper-news.html/3

/132.html/2

Since html is a suffix, it should always be at the end. It comes from solagirl《 The paging link problem when using. html as url suffix 》This article provides us with solutions.

However, the original code only provides the modification method of/% postname%. html.

This article provides the modification method of/% post_id%. html.

 Correction of article pagination link after WordPress setting pseudo static

Add the following code to the current topic functions.php:

 //Suitable for/% post_id%. html pagination link correction class Rewrite_Inner_Page_Links_id{ var $separator; function __construct(){ $this->separator = '/page-'; if( ! is_admin() || defined( 'DOING_AJAX' ) ) : add_filter( 'wp_link_pages_link', array( $this, 'inner_page_link_format' ), 10, 2 ); add_filter( 'get_comments_pagenum_link', array( $this, 'comment_page_link_format' ) ); add_filter( 'redirect_canonical', array( $this, 'cancel_redirect_for_paged_posts' ), 10, 2 ); endif; if( is_admin() ) : add_filter( 'rewrite_rules_array', array( $this, 'pagelink_rewrite_rules' ) ); register_activation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ) ; register_deactivation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ); endif; } function flush_rewrite_rules(){ flush_rewrite_rules(); } //Modify the format of post pagination link function inner_page_link_format( $link, $number ){ if( $number > 1 ){ if( preg_match( '%<a href=".*\.html/\d*"%', $link ) ){ $link = preg_replace( "%(\.html)/(\d*)%", $this->separator. "$2$1", $link ); } } return $link; } //Add redirection rules for the new link format, remove the redirection rules for the original paging links, and prevent repeated inclusion function pagelink_rewrite_rules( $rules ){ foreach ($rules as $rule => $rewrite) { if ( $rule == '([0-9]+).html(/[0-9]+)?/?$' )  { unset($rules[$rule]); } } $new_rule['([0-9]+)('.$this->separator.'([0-9]+))?. html/?$'] = 'index.php?p=$matches[1]&page=$matches[3]'; return $new_rule + $rules; } //Forbid WordPress to jump page links to the original format function cancel_redirect_for_paged_posts( $redirect_url, $requested_url ){ global $wp_query; if( is_single() && $wp_query->get( 'page' ) > 1 ){ return false; } return true; } } new Rewrite_Inner_Page_Links_id();

Tip: After adding code, you need to save the fixed link settings.

Then open the article pagination link again, and it will become similar:

/morning-paper-news/page-2.html

/132/page-2.html

Note: The above code does not comment on the link modification of paging, so I just need to do no research.


At the request of netizens, a new one should be added: in the format of/% category%/% post_id%. html

It seems that people often ask about this format. Here are some ways to modify it:

Replace line 36 of the above code:

 $new_rule['([0-9]+)('.$this->separator.'([0-9]+))?. html/?$'] = 'index.php?p=$matches[1]&page=$matches[3]';

Change to:

 $new_rule['[^/]+/([0-9]+)('.$this->separator.'([0-9]+))?. html/?$'] = 'index.php?p=$matches[1]&page=$matches[3]';

Just added one [^/]+/

The final paging form is:

Classification name/132/page-2.html

After adding and modifying the above code, don't forget to save the fixed link settings in the background, or they will not take effect!


Other fixed links need to be installed rewrite rules inspector The plug-in checks the link regular writing method and modifies the above code.

A theme user asked, why not integrate into the theme? The reason is very simple. Everyone has different forms of fixed links. This code is not universal, and there are still many bugs. For example, the custom taxonomy article cannot be used. After adding the above code, the article may not open

Attachment: Suitable for/% postname%. html link form source code:

open shrink
 //Fix. html Paging Links class Rewrite_Inner_Page_Links{ var $separator; function __construct(){ $this->separator = '/page-'; if( ! is_admin() || defined( 'DOING_AJAX' ) ) : add_filter( 'wp_link_pages_link', array( $this, 'inner_page_link_format' ), 10, 2 ); //  for inner pages add_filter( 'get_comments_pagenum_link', array( $this, 'comment_page_link_format' ) ); add_filter( 'redirect_canonical', array( $this, 'cancel_redirect_for_paged_posts' ), 10, 2 ); endif; if( is_admin() ) : add_filter( 'rewrite_rules_array', array( $this, 'pagelink_rewrite_rules' ) ); register_activation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ) ; register_deactivation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ); endif; } function flush_rewrite_rules(){ flush_rewrite_rules(); } /** *Modify the format of post pagination link * @param string $link * @param int $number * @return string */ function inner_page_link_format( $link, $number ){ if( $number > 1 ){ if( preg_match( '%<a href=".*\.html/\d*"%', $link ) ){ $link = preg_replace( "%(\.html)/(\d*)%", $this->separator. "$2$1", $link ); } } return $link; } /** *Modify comment pagination link * @param string $result * @return string */ function comment_page_link_format( $result ){ // From hello-world.html/comment-page-1#comments to hello-world/comment-page-1.html#comments if( strpos( $result, '.html/' ) !==  false ){ $result = preg_replace( '=([^/]+)(.html)/comment-page-([0-9]{1,})=', "$1/comment-page-$3$2" ,$result ); } return $result; } /** *Add redirection rules for the new link format, remove the redirection rules for the original paging links, and prevent repeated inclusion * *Visiting the original link will return 404 * @param array $rules * @return array */ function pagelink_rewrite_rules( $rules ){ foreach ($rules as $rule => $rewrite) { if ( $rule == '([^/]+).html(/[0-9]+)?/?$' || $ rule == '([^/]+).html/comment-page-([0-9]{1,})/?$' )  { unset($rules[$rule]); } } $new_rule['([^/]+)('.$this->separator.'([0-9]+))?. html/?$'] = 'index.php?name=$matches[1]&page=$matches[3]'; $new_rule['([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$'] = ' index.php?name=$matches[1]&cpage=$matches[2]'; return $new_rule + $rules; } /** *Forbid WordPress to jump page links to the original format * @param string $redirect_url * @param string $requested_url * @return bool */ function cancel_redirect_for_paged_posts( $redirect_url, $requested_url ){ global $wp_query; if( is_single() && $wp_query->get( 'page' ) > 1 ){ return false; } return true; } } new Rewrite_Inner_Page_Links();

The above code may conflict with the No category parents plug-in. Solution:

Open the No category parents plug-in no-category-parents.php file, and find the following on line 54:

  1. //if ( ! isset( $rules['(.+?)-cat/?$'] ) ) { // have to comment this in order to refresh the rules
  2.      global   $wp_rewrite ;
  3.      $wp_rewrite ->flush_rules();
  4. //}

Remove the comment and change it to:

  1. if  ( ! isset(  $rules ['(.+?)-cat/?$'] ) ) {  // have to comment this in order to refresh the rules
  2.      global   $wp_rewrite ;
  3.      $wp_rewrite ->flush_rules();
  4. }

However, I really don't know the use of this default comment. After testing, I didn't find any problems.

If you do not want to use the rewrite rules inspector plug-in to view the link rules, you can add the following code to the topic functions.php, and then open the fixed link settings page to see the link rules.

 add_filter( 'rewrite_rules_array', 'show_rewrite_rules' );  function show_rewrite_rules( $rules ) { echo nl2br( var_export( $rules, true ) ); die; }

 

Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!

 weinxin
My Wechat
Copyright Notice
Please indicate the source and link of the original article reprinted on this site. Thank you for your cooperation!
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 
 Robin
five hundred and ninety-eight million eight hundred and forty-five thousand and six
Comments one hundred and forty-five    Visitors one hundred and thirty-nine    Author six
    •  Twilight farewell
      Twilight farewell zero

      //Add redirection rules for the new link format, remove the redirection rules for the original paging links, and prevent repeated inclusion
      This function is invalid or 301. The original/132.html/2 link

      •  Taizhuang Information
        Taizhuang Information five

        What to do after page 404... :!:

        •  learn
          learn zero

          Come and learn :mrgreen:

          •  Big V
            Big V zero

            Learn

            •  Roasted potato in summer
              Roasted potato in summer two

              Thanks for sharing, which solved the problem I had been struggling with many years ago

              •  Old Toad
                Old Toad zero

                See if it's the result I want, thanks~~~

                •  CPS advertisement
                  CPS advertisement zero

                  Thanks for sharing!!!

                  •  admin
                    admin one

                    Look at the hidden content

                    •  Library 7
                      Library 7 one

                      Just need to take a look

                      •  .
                        . zero

                        Thanks for sharing

                        •  Xiao Quan
                          Xiao Quan zero

                          /%category%/%year%/%second%%post_id%.html
                          How should I modify it?

                          •  Sunlight Tree
                            Sunlight Tree zero

                            Thanks for learning

                            •  twelve million two hundred and thirty-four thousand two hundred and thirty-four
                              twelve million two hundred and thirty-four thousand two hundred and thirty-four zero

                              Come and learn

                              •  SKY8G network
                                SKY8G network zero

                                The website is very good. Don't respond quickly. Give me some support

                                •  Maple
                                  Maple one

                                  After paging is set, access the paging content. The statistics code does not count the number of PVs.

                                  •  bugstack
                                    bugstack zero

                                    Learn

                                    •  Harry
                                      Harry three

                                      Learn

                                      •  Gyroscopic world
                                        Gyroscopic world zero

                                        Learned

                                        •  king
                                          king zero

                                          Thanks for sharing

                                          •  PureDesin
                                            PureDesin zero

                                            Thanks for sharing

                                            •  z
                                              z zero

                                              Thanks for sharing

                                              •  Yephy
                                                Yephy zero

                                                See how to resolve plug-in conflicts

                                                •  Big fish
                                                  Big fish zero

                                                  Expert in learning

                                                  •  Kingfisher flower
                                                    Kingfisher flower zero

                                                    Let me learn!

                                                    •  Big V, look
                                                      Big V, look zero

                                                      It was not easy to finish paging, but/132.html/2 appeared. This article really helped a lot.

                                                      •  Stone.Sam
                                                        Stone.Sam one

                                                        Please consult,/% category%/% post_id%. html
                                                        I have one more layer.

                                                          •  Robin
                                                            Robin

                                                            @ Stone.Sam The modification method has been added to the article

                                                              •  Stone.Sam
                                                                Stone.Sam one

                                                                @ Robin Thank you very much. It's the first time to use WP. There are still many things to learn.

                                                                •  Stone.Sam
                                                                  Stone.Sam one

                                                                  @ Robin This code seems to conflict with Super Static Cache. I used this code, and all pages jump to the home page.

                                                              •  future
                                                                future zero

                                                                There is a problem with the comment pagination, unable to view the previous comments

                                                                •  Two days
                                                                  Two days six

                                                                  After a bit of tossing and turning, the comments are divided into 404 pages :!:

                                                                    •  Robin
                                                                      Robin

                                                                      @ Two days If it works, I would have added it to the theme
                                                                      I tried it. It's not 404, but the link form has not been changed

                                                                        •  Two days
                                                                          Two days six

                                                                          @ Robin I see that apart from comment pages, other pages are OK :mrgreen:

                                                                            •  Robin
                                                                              Robin

                                                                              @ Two days The original code and comments cannot realize the functions he said

                                                                       anonymous

                                                                      Comment

                                                                      Anonymous netizens
                                                                       :?:  :razz:  :sad:  :evil:  :!:  :smile:  :oops:  :grin:  :eek:  :shock:  :???:  :cool:  :lol:  :mad:  :twisted:  :roll:  :wink:  :idea:  :arrow:  :neutral:  :cry:  :mrgreen:

                                                                      Drag the slider to complete validation