原本的wordpress文章如果分页会成如下的结构:
http://www.xyqzmt.cn/1.html/2
通常固定链接都是这样的结构,设为/%postname%.html或者/%post_id%.html 以前我一直无法解决如上的问题,最后放弃挣扎,如果遇到很长的文章全都放在一个页面里面。
今天在知更鸟大佬的博客上找到了结局办法。http://zmingcx.com/pseudo-static-article-page-links.html
将下面代码添加到当前主题 functions.php中:
本文提供一下/%post_id%.html的修改方法。
// 适合/%post_id%.html分页链接修正
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();
}
// 修改post分页链接的格式
function inner_page_link_format( $link, $number ){
if( $number > 1 ){
$link = preg_replace( “%(\.html)/(\d*)%”, $this->separator.”$2$1″, $link );
// 为新的链接格式增加重定向规则,移除原始分页链接的重定向规则,防止重复收录
function pagelink_rewrite_rules( $rules ){
foreach ($rules as $rule => $rewrite) {
if ( $rule == ‘([0-9]+).html(/[0-9]+)?/?$’ ) {
function cancel_redirect_for_paged_posts( $redirect_url, $requested_url ){
if( is_single() && $wp_query->get( ‘page’ ) > 1 ){
new Rewrite_Inner_Page_Links_id();
/morning-paper-news/page-2.html