外贸独立站的底层设计密码WordPress 成品站SaaS建站
当前位置:首页>WordPress建站>WordPress开发>WordPress 修改自定义文章类型的固定链接结构

WordPress 修改自定义文章类型的固定链接结构

关于自定义文章类型固定链接结构,大家可以想回顾一下:

自定义文章类型默认输入的固定链接结构为 /%postname%  。假设我们添加的自定义文章类型为 book ,那么默认输出的 book 文章链接一般为 http://域名/book/slug (slug为标题别名)。如果文章标题是中文(比如:一本好书),而且你没有手动或者使用插件翻译为非中文的 slug (a-nice-book),那么显示的链接就会是 http://域名/book/一本好书 ,这样一来,文章链接的中文部分就会显示成乱码,实在不符合我们的审美标准了。

那么,我们可以将 /%postname% 改为 /%post_id% /%post_id%.html 样式,使用ID来显示。要实现这个目的,可以使用文章开头提到的 Custom Post Type Permalinks 插件。如果你是插件或主题开发者,一般都喜欢直接通过代码定义好默认的固定链接结构。

可以在插件函数文件或主题的functions.php 文件添加下面的代码:

/**
 * 设置 book 这种自定义文章类型的固定链接结构为 ID.html 
 * https://www.wpdaxue.com/custom-post-type-permalink-code.html
 */
add_filter('post_type_link', 'custom_book_link', 1, 3);
function custom_book_link( $link, $post = 0 ){
    if ( $post->post_type == 'book' ){
        return home_url( 'book/' . $post->ID .'.html' );
    } else {
        return $link;
    }
}
add_action( 'init', 'custom_book_rewrites_init' );
function custom_book_rewrites_init(){
    add_rewrite_rule(
        'book/([0-9]+)?.html$',
        'index.php?post_type=book&p=$matches[1]',
        'top' );
    add_rewrite_rule(
        'book/([0-9]+)?.html/comment-page-([0-9]{1,})$',
        'index.php?post_type=book&p=$matches[1]&cpage=$matches[2]',
        'top'
        );
}

以上代码就可以输出形如 /book/123.html 的链接。请将代码中所有 book 替换为你的自定义文章类型。

如果你要同时定义多种自定义文章类型,可以使用下面的代码:

/**
 * 设置多种自定义文章类型的固定链接结构为 ID.html
 * https://www.wpdaxue.com/custom-post-type-permalink-code.html
 */
$mytypes = array(//根据需要添加你的自定义文章类型
    'type1' => 'slug1',
    'type2' => 'slug2',
    'type3' => 'slug3'
    );
add_filter('post_type_link', 'my_custom_post_type_link', 1, 3);
function my_custom_post_type_link( $link, $post = 0 ){
    global $mytypes;
    if ( in_array( $post->post_type,array_keys($mytypes) ) ){
        return home_url( $mytypes[$post->post_type].'/' . $post->ID .'.html' );
    } else {
        return $link;
    }
}
add_action( 'init', 'my_custom_post_type_rewrites_init' );
function my_custom_post_type_rewrites_init(){
    global $mytypes;
    foreach( $mytypes as $k => $v ) {
        add_rewrite_rule(
            $v.'/([0-9]+)?.html$',
            'index.php?post_type='.$k.'&p=$matches[1]',
            'top'
            );
        add_rewrite_rule(
            $v.'/([0-9]+)?.html/comment-page-([0-9]{1,})$',
            'index.php?post_type='.$k.'&p=$matches[1]&cpage=$matches[2]',
            'top'
            );
    }
}

参考资料:http://www.solagirl.net/custom-post-type-permalink.html,感谢 Hello World  反馈及修复评论分页问题。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

WordPress wp_nav_menu()菜单输出菜单描述

2013-9-3 9:13:48

WordPress开发

为WordPress的自定义菜单链接添加栏目图标

2013-9-4 8:38:28

28 条回复 A文章作者 M管理员
  1. 安智网络

    现在4.9的已经有自定义功能了,直接设置一下自定义就可以了

  2. Joanna Jin

    做的企业站,基于后台需要,用Custom Post Type UI自定义了product文章分类,又自定义了taxonomy名为protype,通过加过滤器,实现了网址如下:
    分类:http://www.ytxinhai1.com/product/crusher
    产品:http://www.ytxinhai1.com/product/crusher/3
    但是想给产品加.html,变成http://www.ytxinhai1.com/product/crusher/3.html不知道如何是好啊~求助

  3. Hello World

    这样写会导致文章评论分页失效。

    • Hello World

      请将上面的代码修改为:(解决自定义类型文章评论无法翻页问题。)

      add_rewrite_rule(
      ‘book/([0-9]+)?.html$’,
      ‘index.php?post_type=book&p=$matches[1]’,
      ‘top’
      );
      add_rewrite_rule(
      ‘book/([0-9]+)?.html/comment-page-([0-9]{1,})$’,
      ‘index.php?post_type=book&p=$matches[1]&cpage=$matches[2]’,
      ‘top’
      );

  4. kimsom

    @倡萌 ,遇到一个问题请教下,我建立了一个自定义文章类型store,在固定链接这里出现了问题,如果固定链接设置成 /archives/%post_id% 的时候,store归档页面变成了 /archives/store ,URL中多了一个archives,如果设置成其他的就会正常的是/store/ 这种格式。现在我想用/archives/%post_id%这种固定链接,要怎么将store归档页面URL中的archives去掉呢?请指教!!!

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索