WordPress 修改自定义文章类型的固定链接结构
- WordPress 自定义文章类型 介绍及实例解说(上)
- WordPress 自定义文章类型 介绍及实例解说(下)
- WordPress快速添加多个自定义文章类型
- WordPress自定义文章类型的固定链接设置插件:Custom Post Type Permalinks
自定义文章类型默认输入的固定链接结构为 /%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 文件添加下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * 设置 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 替换为你的自定义文章类型。
如果你要同时定义多种自定义文章类型,可以使用下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * 设置多种自定义文章类型的固定链接结构为 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 反馈及修复评论分页问题。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
现在4.9的已经有自定义功能了,直接设置一下自定义就可以了
做的企业站,基于后台需要,用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不知道如何是好啊~求助
试试
https://www.wpdaxue.com/custom-post-type-permalinks.html
或
https://www.wpdaxue.com/custom-post-type-permalink-code.html
谢谢倡萌回复。两种都试过,but出现了404,我在网上查过很多资料,没解决~
这样写会导致文章评论分页失效。
请将上面的代码修改为:(解决自定义类型文章评论无法翻页问题。)
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’
);
@倡萌 ,遇到一个问题请教下,我建立了一个自定义文章类型store,在固定链接这里出现了问题,如果固定链接设置成 /archives/%post_id% 的时候,store归档页面变成了 /archives/store ,URL中多了一个archives,如果设置成其他的就会正常的是/store/ 这种格式。现在我想用/archives/%post_id%这种固定链接,要怎么将store归档页面URL中的archives去掉呢?请指教!!!
没深入研究过 这个问题,建议你试试 http://www.wpdaxue.com/custom-post-type-permalinks.html 这个插件
使用这个插件并不能把archives去掉