巧妙美化静态化文章分页链接结构

前天和大家分享的“如何实现Wordpress文章分页”反响很好,但现在很多WPer都静态化了自己的的Blog,Wopus中文平台也一样,采用了“%postname%.html”这样的链接形式,分页后就变成了“postname.html/2”的形式,相当不美观,我们需要“postname-2.html”这样的形式,来,动两个小手术:

  1. 修改wp-includes/rewrite.php 文件
  2. 找到 $rewrite = array_merge($rewrite, array($match => $query))
    在这行代码之前添加以下代码:

    if (strpos($match, ‘.html’) !== false && strpos($query, ‘&page=’) !== false) {
    $match = str_replace(‘(/[0-9]+)?/?$’, ‘$’, $match);
    $rewrite = array_merge($rewrite, array(str_replace(‘([^/]+).html’, ‘([^/]+)-([0-9]+).html’, $match) => $query));
    }

  3. wp-includes/post-template.php 文件
  4. 找到wp_link_pages函数,用以下代码替代之:


    < ?php /* 修改页面链接 */ function alter_page_link($link, $pagenum){ return str_replace(".html", "-$pagenum.html", $link); } /* 包含在 / *=================* / 之间的代码作过修改 */ function wp_link_pages($args = '') { $defaults = array( 'before' => ‘

    ‘ . __(‘Pages:’), ‘after’ => ‘

    ‘,
    ‘next_or_number’ => ‘number’, ‘nextpagelink’ => __(‘Next page’),
    ‘previouspagelink’ => __(‘Previous page’), ‘pagelink’ => ‘%’,
    ‘more_file’ => ”, ‘echo’ => 1
    );

    $r = wp_parse_args( $args, $defaults );
    extract( $r, EXTR_SKIP );

    global $post, $id, $page, $numpages, $multipage, $more, $pagenow;
    if ( $more_file != ” )
    $file = $more_file;
    else
    $file = $pagenow;

    $output = ”;

    /*=================*/
    /* 优化一下,太多重复调用了-_-! */
    $permalink_structure = get_option(‘permalink_structure’);
    $not_public_post = in_array($post->post_status, array(‘draft’, ‘pending’));
    $permalink = get_permalink();

    /* 判断永久链接格式是否支持静态化 */
    $using_static_permalink = strpos($permalink, ‘.html’) !== false ? true : false;
    /*=================*/

    if ( $multipage ) {
    if ( ‘number’ == $next_or_number ) {
    $output .= $before;
    for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) { $j = str_replace('%',"$i",$pagelink); $output .= ' '; if ( ($i != $page) || ((!$more) && ($page==1)) ) { if ( 1 == $i ) { $output .= '‘;
    } else {
    /*=================*/
    if ( ” == $permalink_structure || $not_public_post ) {
    $output .= ‘
    ‘;
    } else {
    if (!$using_static_permalink) {
    $output .= ‘
    ‘;
    } else {
    $output .= ‘
    ‘;
    }
    }
    /*=================*/
    }
    }
    $output .= $j;
    if ( ($i != $page) || ((!$more) && ($page==1)) )
    $output .= ‘
    ‘;
    }
    $output .= $after;
    } else {
    if ( $more ) {
    $output .= $before;
    $i = $page – 1;
    if ( $i && $more ) {
    if ( 1 == $i ) {
    $output .= ‘‘ . $previouspagelink . ‘‘;
    } else {
    /*=================*/
    if ( ” == $permalink_structure || $not_public_post ) {
    $output .= ‘‘ . $previouspagelink . ‘‘;
    } else {
    if (!$using_static_permalink) {
    $output .= ‘‘ . $previouspagelink . ‘‘;
    } else {
    $output .= ‘‘ . $previouspagelink . ‘‘;
    }
    }
    /*=================*/
    }
    }
    $i = $page + 1;
    if ( $i < = $numpages && $more ) { if ( 1 == $i ) { $output .= '‘ . $nextpagelink . ”;
    } else {
    /*=================*/
    if ( ” == $permalink_structure || $not_public_post ) {
    $output .= ‘
    ‘ . $nextpagelink . ‘‘;
    } else {
    if (!$using_static_permalink) {
    $output .= ‘‘ . $nextpagelink . ‘‘;
    } else {
    $output .= ‘‘ . $nextpagelink . ‘‘;
    }
    }
    /*=================*/
    }
    }
    $output .= $after;
    }
    }
    }

    if ( $echo )
    echo $output;

    return $output;
    }
    ?>

以上步骤在Wordpress2.6下测试通过,代码来自Voidman
注意:
1、当你修改完成后,可能需要到后台设置里重新修改一次“永久链接形式”,无需改动设置,只需点一下“更新”。否则会出现404错误。
2、若没有静态化、伪静态化过Wordpress,也就是没有采用”%postname%.html”这样的链接形式,请不要这样修改,否则会出现“404”错误。

类似文章

19条评论

  1. Parse error: syntax error, unexpected ‘<‘ in /home/purpure/public_html/jeans/wp-includes/post-template.php on line 160

    按照修改,出现错误!

  2. 使用Ultraedit编辑:先把一大段wp_link_pages函数删掉,然后在删掉的位置粘贴你给的代码,再把前面的序号删掉,然后另存为时选择utf-8无dom模式保存,最后上传文件。

    可惜还是出错,错误和之前一模一样!不知怎么搞的?
    求助!

  3. 为什么我的标题是Forex Market Review 05/08/2009 这种形式的就出现问题呢Not Found, Error 404
    The page you are looking for no longer exists.

已关闭评论。