如何重写自定义文章类型的固定链接

WordPress的自定义文章类型(Custom Post Type)允许我们创建一种有别于blog posts但又能分组管理的文章类型,Custom Post Type在属性上更接近于Page,而非Post,它们的固定链接(Permalink)也不受制于后台-设置-固定链接中为Post规定的链接格式。

如何注册自定义文章类型

注册自定义文章类型用到的函数是register_post_type,可以套用官方文档的代码示例,将下面的代码放到主题的functions.php中,到后台查看菜单,就会发现多了一个选项卡叫“Books”。

[code]

add_action( ‘init’, ‘codex_custom_init’ );
function codex_custom_init() {
$labels = array(
‘name’ => _x(‘Books’, ‘post type general name’),
‘singular_name’ => _x(‘Book’, ‘post type singular name’),
‘add_new’ => _x(‘Add New’, ‘book’),
‘add_new_item’ => __(‘Add New Book’),
‘edit_item’ => __(‘Edit Book’),
‘new_item’ => __(‘New Book’),
‘all_items’ => __(‘All Books’),
‘view_item’ => __(‘View Book’),
‘search_items’ => __(‘Search Books’),
‘not_found’ =>  __(‘No books found’),
‘not_found_in_trash’ => __(‘No books found in Trash’),
‘parent_item_colon’ => ”,
‘menu_name’ => ‘Books’

);
$args = array(
‘labels’ => $labels,
‘public’ => true,
‘publicly_queryable’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘query_var’ => true,
‘rewrite’ => true,
‘capability_type’ => ‘post’,
‘has_archive’ => true,
‘hierarchical’ => false,
‘menu_position’ => null,
‘supports’ => array( ‘title’, ‘editor’, ‘author’, ‘thumbnail’, ‘excerpt’, ‘comments’ )
);
register_post_type(‘book’,$args);
}

[/code]

自定义文章类型的默认固定链接格式

Custom Post Type默认的固定链接格式是‘post-slug/postname’,如果没有指定slug,则用post type作为slug,本例中没有指定,所有post slug就是book。
与固定链接相关的参数有rewrite、和slug
rewrite参数指定是否开启固定链接功能,rewrite默认是true,如果设置成false,假设我创建了一个book类型的文章,标题是“Harry Potter Book”,产生的链接如下:
http://mydomain.com/?post_type=book&p=33
如果rewrite为true,链接为
http://mydomain.com/book/harry-potter-book
结尾是否有反斜杠,取决于设置-固定链接中的格式结尾是否有反斜杠,建议这里结尾不要带反斜杠,否则可能出现
http://mydomain.com/book/harry-potter-book
http://mydomain.com/book/harry-potter-book/
指向同一个地址的情况,对搜索引擎不友好。

如何修改自定义文章类型的固定链接格式

假设我们创建了book类型的文章,并且用中文当做文章标题,那么默认产生的链接也将是中文,中文链接通常会编码,比较长,分享不方便。你可以手动输入英文slug,也可以通过修改固定链接格式让了链接更简短。
例如,我想让http://mydomain.com/book/harry-potter-book变成http://mydomain.com/book/33.html,也就是用文章的ID作为链接格式。
要达到这个目的:
  1. 创建新的rewrite规则翻译URL
  2. 添加filter(post_type_link),当get_the_permalink()函数调用时,返回正确的链接格式
下面有两段代码,都可以实现这个要求,代码加到functions.php中,并且要到后台-设置-固定链接中重新保存固定链接,代码才能生效。
代码段1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
add_action(‘init’, ‘custom_book_rewrite’);
function custom_book_rewrite() {
  global $wp_rewrite;
  $queryarg = ‘post_type=book&p=’;
  $wp_rewrite->add_rewrite_tag(‘%qid%’, ‘([^/]+)’, $queryarg);
  $wp_rewrite->add_permastruct(‘book’, ‘/book/%qid%.html’, false);
}
add_filter(‘post_type_link’, ‘custom_book_permalink’, 1, 3);
function custom_book_permalink($post_link, $post = 0) {
  global $wp_rewrite;
  if ( $post->post_type == ‘book’ ){
      $post = &get_post($id);
      if ( is_wp_error( $post ) )
        return $post;
      $newlink = $wp_rewrite->get_extra_permastruct(‘book’);
      $newlink = str_replace(“%qid%”, $post->ID, $newlink);
      $newlink = home_url(user_trailingslashit($newlink));
      return $newlink;
    } else {
        return $post_link;
    }
}
代码段2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
add_filter(‘post_type_link’, ‘custom_book_link’, 1, 3);
function custom_book_link( $link, $post = 0 ){
    if ( $post->post_type == ‘book’ ){
        return home_url( ‘book/’ . $post->ID .’.html’ );
    } else {
        return $link;
    }
}
add_action( ‘init’, ‘custom_book_rewrites_init’ );
function custom_book_rewrites_init(){
    add_rewrite_rule(
        ‘book/([0-9]+)?.html$’,
        ‘index.php?post_type=book&p=$matches[1]’,
        ‘top’ );
}

 针对多个Custom Post Type

将代码段2修改一下,就可以同时修改多个自定义文章类型的固定链接
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$mytypes = array(
    ‘type1’ => ‘slug1’,
    ‘type2’ => ‘slug2’,
    ‘type3’ => ‘slug3’
);
add_filter(‘post_type_link’, ‘custom_book_link’, 1, 3);
function custom_book_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’, ‘custom_book_rewrites_init’ );
function custom_book_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’ );
    }
}
$mytypes数组存储需要更改固定链接的Custom Post Type和它们的固定链接前缀,例如类型为type1的固定链接将是
http://mydomain.com/slug1/875.html

本文转载于:https://www.solagirl.net/custom-post-type-permalink.html

晴朗网络专注WordPress建站,提供资源和经验分享,欢迎咨询交流!
晴朗网络 » 如何重写自定义文章类型的固定链接