在 Wordpress 自定义内容类型、分类、标签 这篇文章中,我们知道了如何自定义文章类型,在 Wordpress 的伪静态设置 这篇文章中讲了如何开启 wordpress 的伪静态,但是当你细心一点,会发现自定义的内容类型,伪静态貌似没有生效,访问文章的话,链接会类似 http://localhost:8080/sites/通义千问
这样,还会出现中文,和我们预想的 http://localhost:8080/sites/1.html
有较大的差异,那么应该如何对自定义内容类型做伪静态呢?
直接上代码,详细可以看这篇介绍WordPress 修改自定义文章类型的固定链接结构 – WordPress大学,将下面的代码放到你的插件入口,或者主题的 functions.php
中,能调用到就可以。
--------------------------------------[自定义文章]--------------------------------------------------------------
/**
* 设置自定义文章类型的固定链接结构为 ID.html
* ---------------------------------------------------------------------------------------------------------------
* https://www.wpdaxue.com/custom-post-type-permalink-code.html
*/
$_iotpostypes = array(
'sites' => io_get_option('sites_rewrite','sites','post'),
);
global $IOPOSTTYPE;
$IOPOSTTYPE = apply_filters( 'io_rewrites_post_type', $_iotpostypes );
if( get_option('permalink_structure') ):
if( io_get_option('rewrites_end',false) ){ //删除链接结尾 ‘/’
add_filter( 'user_trailingslashit', 'io_custom_post_trailingslashit', 10, 2 );
}
if( io_get_option('rewrites_types','post_id') == 'post_id' ) {
add_filter( 'post_type_link', 'io_custom_post_type_link_id', 1, 2 );
add_action( 'rewrite_rules_array', 'io_custom_post_type_rewrites_init_id' );
} elseif( io_get_option('rewrites_types','postname') == 'postname' && io_get_option('rewrites_end',true) ) {// rewrites_end 为 false 使用wp默认规则
add_filter( 'post_type_link', 'io_custom_post_type_link_name', 1, 2 );
add_action( 'rewrite_rules_array', 'io_custom_post_type_rewrites_init_name' );
}
endif;
/**
* 主题设置加 .html ,但是wp设置里没加,自动删掉自定义文章类型结尾的斜杠,改 sites/123.html/ 为 sites/123.html
* 加 .html 后缀后,分页 123.html/2 结尾加斜杠,123.html/2/
* @param string $string
* @param string $type_of_url
* @return string
*/
function io_custom_post_trailingslashit($string, $type_of_url){
if(is_admin()){
return $string;
}
global $IOPOSTTYPE, $is_new_page, $post, $wp_query;
if( $IOPOSTTYPE && $post && in_array( $post->post_type,array_keys($IOPOSTTYPE) ) && isset($wp_query->query_vars['page']) && $wp_query->query_vars['page']==0){
return untrailingslashit( $string );
}
if( (!$is_new_page && isset($wp_query->query_vars['page']) && $wp_query->query_vars['page']!=0) ){ // 如果没用启用 $is_new_page 则文章分页结尾加 ‘/’
return trailingslashit( $string );
}
return $string;
}
// ID
function io_custom_post_type_link_id( $link, $post ){
global $IOPOSTTYPE, $wp_rewrite;
if ( in_array( $post->post_type,array_keys($IOPOSTTYPE) ) ){
$slashes = $wp_rewrite->use_trailing_slashes?'/':'';
return home_url().'/'.$IOPOSTTYPE[$post->post_type].'/' . $post->ID . (io_get_option('rewrites_end',true) ? '.html' : $slashes);
} else {
return $link;
}
}
function io_custom_post_type_rewrites_init_id( $rules ){
global $IOPOSTTYPE, $is_new_page;
$new_rule = array();
$post_rule = '/([0-9]+)?(?:/([0-9]+))?/?$';
$comment_rule = '/([0-9]+)?/comment-page-([0-9]{1,})/?$';
if(io_get_option('rewrites_end',true)){
if($is_new_page){
$post_rule = '/([0-9]+)?-?([0-9]+)?.html/?$';
$comment_rule = '/([0-9]+)?/comment-page-([0-9]{1,}).html/?$';
}else{
$post_rule = '/([0-9]+)?.html(?:/([0-9]+))?/?$';
$comment_rule = '/([0-9]+)?.html/comment-page-([0-9]{1,})/?$';
}
}
foreach( $IOPOSTTYPE as $k => $v ) {
foreach ($rules as $rule => $rewrite) {
if ($rule == $v.'/([^/]+)/comment-page-([0-9]{1,})/?$' ||
$rule == $v.'/([^/]+)(?:/([0-9]+))?/?$'
) {
unset($rules[$rule]);
}
}
$new_rule[ $v.$comment_rule ] = 'index.php?post_type='.$k.'&p=$matches[1]&cpage=$matches[2]';
$new_rule[ $v.$post_rule ] = 'index.php?post_type='.$k.'&p=$matches[1]&page=$matches[2]';
}
return $new_rule + $rules;
}
// post_name
// 只处理 html 结尾规则,自定义文章系统默认为 /%postname%/
function io_custom_post_type_link_name( $link, $post ){
global $IOPOSTTYPE;
if ( in_array( $post->post_type,array_keys($IOPOSTTYPE) ) ){
return home_url().'/'.$IOPOSTTYPE[$post->post_type].'/' . $post->post_name . '.html';
} else {
return $link;
}
}
function io_custom_post_type_rewrites_init_name( $rules ){
$new_rule = array();
global $IOPOSTTYPE, $is_new_page;
$lang = io_get_lang_rules();
foreach( $IOPOSTTYPE as $k => $v ) {
$old = array(
$v.'/([^/]+)/comment-page-([0-9]{1,})/?$',
$v.'/([^/]+)(?:/([0-9]+))?/?$'
);
if($lang){
$old[] = $lang . $v . '/([^/]+)/comment-page-([0-9]{1,})/?$';
$old[] = $lang . $v . '/([^/]+)(?:/([0-9]+))?/?$';
}
foreach ($rules as $rule => $rewrite) {
if (in_array($rule, $old)) {
unset($rules[$rule]);
}
}
if($is_new_page){
$new_rule[$v . '/([^/]+)?/comment-page-([0-9]{1,}).html/?$'] = 'index.php?' . $k . '=$matches[1]&cpage=$matches[2]';
$new_rule[$v . '/([^/]+)?(?:/([0-9]+))?.html/?$'] = 'index.php?' . $k . '=$matches[1]&page=$matches[2]';
if($lang){
$new_rule[$lang . $v . '/([^/]+)?/comment-page-([0-9]{1,}).html/?$'] = 'index.php?lang=$matches[1]&' . $k . '=$matches[2]&cpage=$matches[3]';
$new_rule[$lang . $v . '/([^/]+)?(?:/([0-9]+))?.html/?$'] = 'index.php?lang=$matches[1]&' . $k . '=$matches[2]&page=$matches[3]';
}
}else{
$new_rule[$v . '/([^/]+)?.html/comment-page-([0-9]{1,})/?$'] = 'index.php?' . $k . '=$matches[1]&cpage=$matches[2]';
$new_rule[$v . '/([^/]+)?.html(?:/([0-9]+))?/?$'] = 'index.php?' . $k . '=$matches[1]&page=$matches[2]';
if($lang){
$new_rule[$lang . $v . '/([^/]+)?.html/comment-page-([0-9]{1,})/?$'] = 'index.php?lang=$matches[1]&' . $k . '=$matches[2]&cpage=$matches[3]';
$new_rule[$lang . $v . '/([^/]+)?.html(?:/([0-9]+))?/?$'] = 'index.php?lang=$matches[1]&' . $k . '=$matches[2]&page=$matches[3]';
}
}
}
return $new_rule + $rules;
}
// --------------------------------------[自定义文章] END--------------------------------------------------------------
function io_get_option($option, $default = null, $key = ''){
global $io_get_option;
if ($io_get_option) {
$options = $io_get_option;
} else {
$options = get_option('io_get_option');
$io_get_option = $options;
}
$_v = $default;
if (isset($options[$option])) {
if ($key) {
$_v = isset($options[$option][$key]) ? $options[$option][$key] : $default;
} else {
$_v = $options[$option];
}
}
$_v = _iol($_v, $option, isset($options['m_language']) ? $options['m_language'] : false);
return $_v;
}
发表回复