html 使用符号设置固定链接,关于dw-question-answer的调试和固定链接id.html设置

关于问答插件,我想大家都不陌生,wp大学介绍了很多种,这里我们重点说下关于dw-question-answer的设置问题,通过调试,发现这款插件功能很全,可以设置权限,投票,状态等等,但对于设置了页面html结尾固定链接的朋友会发现,wp大学给出的id.html设置方法并不完整,对于单个问题页面链接是有效的,但查看sitemap就会发现,每个回答的链接是中文模式,那么一长串的链接看着很是别扭,这里出下解决办法。话不多说,先来个效果图吧。

897a53be6517ee47a4fbede6fd3ee691.png

安装过dw-question-answe插件的朋友都会发现,每个问题单页链接都是问题标题,都很长,这对于页面的分享来说,甚是别扭,wp大学给出的固定链接id.html方法如下。添加在主题functions.php文件内即可。

/** * 自定义问答页面的固定链接结构为 id.html * https://www.wpdaxue.com/custom-post-type-permalink-code.html */add_filter('post_type_link', 'custom_qa_link', 1, 3);function custom_qa_link( $link, $post = 0 ){if ( $post->post_type == 'dwqa-question' ){return home_url( 'question/' . $post->ID .'.html' );} else {return $link;}}add_action( 'init', 'custom_qa_rewrites_init' );function custom_qa_rewrites_init(){add_rewrite_rule('question/([0-9]+)?.html$','index.php?post_type=dwqa-question&p=$matches[1]','top' );}如果设置无效或出现404,在后台设置,固定链接处保存更新下静态规则即可。

但上面的方法对于回答页面并不管用,可能回答页面并不单独展示,所以wp大学并未添加回答页面的id.html处理,但是站点地图 sitemap 处查看回答链接那么一长串很是别扭,于是动手改动了一下。

/** * 自定义问答页面的固定链接结构为 id.html * https://www.wpdaxue.com/custom-post-type-permalink-code.html */add_filter('post_type_link', 'custom_qa_link', 1, 3);function custom_qa_link( $link, $post = 0 ){if ( $post->post_type == 'dwqa-question' ){return home_url( 'question/' . $post->ID .'.html' );}if ( $post->post_type == 'dwqa-answer' ){return home_url( 'hd/' . $post->ID .'.html' );}else {return $link;}}add_action( 'init', 'custom_qa_rewrites_init' );function custom_qa_rewrites_init(){add_rewrite_rule('question/([0-9]+)?.html$','index.php?post_type=dwqa-question&p=$matches[1]','top' );add_rewrite_rule('hd/([0-9]+)?.html$','index.php?post_type=dwqa-answer&p=$matches[1]','top' );}如果设置无效或出现404,在后台设置,固定链接处保存更新下静态规则即可。

如果页面开启了.html结尾,请把以上固定链接规则加在页面.html规则上方 ,或者 添加在主题functions.php文件 顶部,保存后来设置固定链接处确定保存即可。

举报/反馈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值