wordpress自定义文章类型的文章固定链接怎么改成⼀%postname%.html形式的?不要插件方法实现的。

2025-03-23 19:31:47
推荐回答(1个)
回答1:

楼上的确实没看懂问题,这个问题比较难搞目前我知道的post_id形式的自定义文章固定链接可以这么设置。postname的你可以到插件里看看,把这种形式的重写规则自己整出来自己代码实现。
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;
}
}