如何使 Wordpress 自定义 Post 类型固定链接
How to make Wordpress Custom Post Type Permalink
我将使用此永久links 设置(e.g. /%second%%minute%%hour%%day%)
现在默认 post link > www."mywebsite".com/12331406
我有一个post_type
简单的:onur
onur 的 posts 使用此 permalink > www."mywebsite".com/onur/post-title
但我需要 > www."mywebsite".com/12331406
or/else > www."mywebsite".com/onur/12331406
没问题。
任何帮助...?
我已经按照你的要求做了,但我不知道这是否适合你
只需将此添加到 functions.php 文件
add_filter('post_type_link', 'woorei_33551_custom_listing_permalink', 1, 3);
function woorei_33551_custom_listing_permalink($post_link, $post = 0, $leavename) {
if ( $post->post_type == 'CUSTOM_POST_TYPE' ) {
//You can whatever you want to instead of post id
return home_url( '/' . $post->ID);
}
else {
return $post_link;
}
}
add_action( 'init', 'woorei_33551_rewrites_init' );
function woorei_33551_rewrites_init(){
add_rewrite_rule(
'([0-9]+)?$',
'index.php?post_type=CUSTOM_POST_TYPE&p=$matches[1]',
'top' );
}
我将使用此永久links 设置(e.g. /%second%%minute%%hour%%day%)
现在默认 post link > www."mywebsite".com/12331406
我有一个post_type
简单的:onur
onur 的 posts 使用此 permalink > www."mywebsite".com/onur/post-title
但我需要 > www."mywebsite".com/12331406
or/else > www."mywebsite".com/onur/12331406
没问题。
任何帮助...?
我已经按照你的要求做了,但我不知道这是否适合你
只需将此添加到 functions.php 文件
add_filter('post_type_link', 'woorei_33551_custom_listing_permalink', 1, 3);
function woorei_33551_custom_listing_permalink($post_link, $post = 0, $leavename) {
if ( $post->post_type == 'CUSTOM_POST_TYPE' ) {
//You can whatever you want to instead of post id
return home_url( '/' . $post->ID);
}
else {
return $post_link;
}
}
add_action( 'init', 'woorei_33551_rewrites_init' );
function woorei_33551_rewrites_init(){
add_rewrite_rule(
'([0-9]+)?$',
'index.php?post_type=CUSTOM_POST_TYPE&p=$matches[1]',
'top' );
}