如何重写自定义文章类型的固定链接
如何注册自定义文章类型
[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]
自定义文章类型的默认固定链接格式
如何修改自定义文章类型的固定链接格式
-
创建新的rewrite规则翻译URL
-
添加filter(post_type_link),当get_the_permalink()函数调用时,返回正确的链接格式
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;
}
}
|
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
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’ );
}
}
|
本文转载于:https://www.solagirl.net/custom-post-type-permalink.html