WordPress自定义文章类型打开评论链接404怎么办?

WordPress国产主题推荐

比如 boke112百科的“问答”就是采用自定义文章类型,在评论回复邮件中点击查看完整内容的评论链接地址(如 https://boke112.com/article/p5089.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'
);
文章创作不易,期待您的评分

本文地址:https://boke112.com/article/p5243.html

版权声明:本文为原创文章,版权归 boke112百科 所有,欢迎分享本文,转载请保留出处!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
wu