比如 boke112 导航的博客问答就是采用自定义文章类型,在评论回复邮件中点击查看完整内容的评论链接地址(如 https://boke112.com/post/5089.html/comment-page-1/#comment-61603)就会出现404 页面,但是其他文章页、页面的评论链接地址却显示正常,造成这个原因是因为 boke112 导航使用纯代码实现 WordPress 自定义文章类型的固定链接结构,当初的代码只考虑到将自定义文章类型的 URL 结果改写为 ID.html 结果,尚未考虑到评论地址的跳转问题,所以只需要将代码添加上评论地址即可。比如自定义文章类型 wenda,slug 为 bkwd,那么包含评论链接跳转的完整的代码如下:
- add_filter(‘post_type_link’, ‘custom_wenda_link’, 1, 3);
- function custom_wenda_link( $link, $post = 0 ){
- if ( $post->post_type == ‘wenda’ ){
- return home_url( ‘bkwd/’ . $post->ID .’.html’ );
- } else {
- return $link;
- }
- }
- add_action( ‘init’, ‘custom_wenda_rewrites_init’ );
- function custom_wenda_rewrites_init(){
- add_rewrite_rule(
- ‘bkwd/([0-9]+)?.html$’,
- ‘index.php?post_type=wenda&p=$matches[1]’,
- ‘top’ );
- add_rewrite_rule(
- ‘bkwd/([0-9]+)?.html/comment-page-([0-9]{1,})$’,
- ‘index.php?post_type=book&p=$matches[1]&cpage=$matches[2]’,
- ‘top’
- );
- }
其实就是比平时的代码多了评论的链接改写而已,具体增加的代码如下:
- add_rewrite_rule(
- ‘bkwd/([0-9]+)?.html/comment-page-([0-9]{1,})$’,
- ‘index.php?post_type=book&p=$matches[1]&cpage=$matches[2]’,
- ‘top’
- );