?
Wordpress 固定鏈接形式:
wordpress 固定鏈接一共幾種形式 我們到底應(yīng)該用哪一個(gè)?
1秒裕、/%year%/%monthnum%/%day%/%postname%/
2、/%year%/%monthnum%/%postname%/
3肮塞、/%year%/%monthnum%/%day%/%postname%.html
4、/%year%/%monthnum%/%postname%.html
5姻锁、/%category%/%postname%.html
6枕赵、/%post_id%.html
7、/%post_id%/
8位隶、/%postname%/
9拷窜、/%postname%.html
上網(wǎng)攻略了一番 大家常用的有兩種
1. /%post_id%.html
2. /%postname%.html
博主使用的是/%post_id%.html
【實(shí)現(xiàn)方法】
第一步:wordPress后臺(tái) 設(shè)置-> 固定鏈接
20170627154529.png
第二步:向function.php中添加如下代碼
/**
* 設(shè)置多種自定義文章類型的固定鏈接結(jié)構(gòu)為 ID.html
* https://www.wpdaxue.com/custom-post-type-permalink-code.html
*/
$mytypes = array(//根據(jù)需要添加你的自定義文章類型
'type1' => 'slug1',
'type2' => 'slug2',
'type3' => 'slug3'
);
add_filter('post_type_link', 'my_custom_post_type_link', 1, 3);
function my_custom_post_type_link( $link, $post = 0 ){
global $mytypes;
if ( in_array( $post->post_type,array_keys($mytypes) ) ){
return home_url( $mytypes[$post->post_type].'/' . $post->ID .'.html' );
} else {
return $link;
}
}
add_action( 'init', 'my_custom_post_type_rewrites_init' );
function my_custom_post_type_rewrites_init(){
global $mytypes;
foreach( $mytypes as $k => $v ) {
add_rewrite_rule(
$v.'/([0-9]+)?.html$',
'index.php?post_type='.$k.'&p=$matches[1]',
'top'
);
add_rewrite_rule(
$v.'/([0-9]+)?.html/comment-page-([0-9]{1,})$',
'index.php?post_type='.$k.'&p=$matches[1]&cpage=$matches[2]',
'top'
);
}
}
第三步:檢查主機(jī)是否支持偽靜態(tài)
這里本博客用的是LNMP
vi /usr/local/nginx/conf/nginx.conf
在root /home/xxx/xxx;這一行下面添加:
include wordpress.conf;
wordpress.conf為偽靜態(tài)文件
如需要其他偽靜態(tài)文件 自己創(chuàng)建個(gè)并上傳到/usr/local/nginx/conf/ 目錄下
并include 偽靜態(tài).conf; 修改完保存
執(zhí)行:
/etc/init.d/nginx restart
重啟生效
如果報(bào)錯(cuò)可能是添加有誤或偽靜態(tài)規(guī)則有誤。
?