预计阅读时间: 2 分钟

要想加这个问答的固定链接结构设置为 id.html 结构,只需将下面的代码添加到当前主题的 functions.php :


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后台 – 设置 – 固定链接,保存一遍这里的设置即可。如果还不行,很可能是你的主机不支持伪静态,请联系你的主机商。

此文章对你有帮助吗? 已有 1 人说这篇文章是有用的。