更改固定链接到自定义后类型

问题描述:

我试图让我的永久链接是这样的:更改固定链接到自定义后类型

http://example.com/projects/1234 

默认情况下,它看起来像这样:

http://example.com/projects/title 

我尝试设置“蛞蝓“到‘项目/%POST_ID%’,但随后是这样的:

http://example.com/projects/%post_id%/title 

有没有办法向塞设置为我的自定义弹头‘/’的该帖子的ID?任何帮助表示赞赏。

我给你的礼物:只需将“property”替换为你的自定义帖子类型的名称即可。走进你的主题的功能.php

add_filter('post_type_link', 'property_post_type_link', 1, 3); 

function property_post_type_link($link, $post = 0){ 
    if ($post->post_type == 'property'){ 
     return home_url('property/' . $post->ID); 
    } else { 
     return $link; 
    } 
} 

add_action('init', 'property_rewrites_init'); 

function property_rewrites_init(){ 
    add_rewrite_rule(
     'property/([0-9]+)?$', 
     'index.php?post_type=property&p=$matches[1]', 
     'top'); 
} 
+0

谢谢,完美的作品。但是,就像注释那样,没有第二个参数被传递给property_post_type_link函数。相反,我不得不在函数的开头使用全局$ post。 – ngreenwood6

'项目'是一个类别或标签等?

如果项目是类别或标记,则类似下面的内容应该可以工作。

/%category%/%post_id%/