腾讯云服务器优惠活动华纳云香港服务器

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

WordPress主题推荐

比如 boke112 导航的博客问答就是采用自定义文章类型,在评论回复邮件中点击查看完整内容的评论链接地址(如 https://boke112.com/post/5089.html/comment-page-1/#comment-61603)就会出现404 页面,但是其他文章页、页面的评论链接地址却显示正常,造成这个原因是因为 boke112 导航使用纯代码实现 WordPress 自定义文章类型的固定链接结构,当初的代码只考虑到将自定义文章类型的 URL 结果改写为 ID.html 结果,尚未考虑到评论地址的跳转问题,所以只需要将代码添加上评论地址即可。比如自定义文章类型 wenda,slug 为 bkwd,那么包含评论链接跳转的完整的代码如下:

  1. add_filter(‘post_type_link’, ‘custom_wenda_link’, 1, 3);
  2. function custom_wenda_link( $link$post = 0 ){
  3.     if ( $post->post_type == ‘wenda’ ){
  4.         return home_url( ‘bkwd/’ . $post->ID .’.html’ );
  5.     } else {
  6.         return $link;
  7.     }
  8. }
  9. add_action( ‘init’, ‘custom_wenda_rewrites_init’ );
  10. function custom_wenda_rewrites_init(){
  11.     add_rewrite_rule(
  12.         ‘bkwd/([0-9]+)?.html$’,
  13.         ‘index.php?post_type=wenda&p=$matches[1]’,
  14.         ‘top’ );
  15.     add_rewrite_rule(
  16.         ‘bkwd/([0-9]+)?.html/comment-page-([0-9]{1,})$’,
  17.         ‘index.php?post_type=book&p=$matches[1]&cpage=$matches[2]’,
  18.         ‘top’
  19.         );
  20. }

其实就是比平时的代码多了评论的链接改写而已,具体增加的代码如下:

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

本文地址:https://boke112.com/post/5243.html

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