自定义帖子类型URL重写?
2 个回答
- 投票数
-
- 2012-05-25
注册自定义帖子类型时,必须指定重写规则不应以现有的URL结构开头.
简而言之,这意味着您
register_post_type
调用中的这一行:'rewrite'=>array('slug'=>'projects'),
应该变成这样:
'rewrite'=>array('slug'=>'projects','with_front'=>false),
有关详细信息,请从
register_post_type上的 codex条目中检出
rewrite
参数.edit:只需确保在更新代码后,通过访问"设置">"永久链接"即可刷新重写规则.否则,您仍然会看到旧的链接.
When you register the custom post type, you have to specify that the rewrite rule shouldn't be prepended with the existing URL structure.
In short, this means that this line in your
register_post_type
call:'rewrite' => array('slug' => 'projects'),
should turn into this:
'rewrite' => array('slug' => 'projects','with_front' => false),
For more info, check out the
rewrite
argument from the codex entry onregister_post_type
edit: just make sure that, after updating the code, you flush the rewrite rules by visiting Settings > Permalinks. Otherwise you'll still see the old links.
-
辉煌的谢谢!为了澄清,我需要执行刷新规则的所有步骤是转到"设置"->"永久链接"页面,然后单击"保存更改",对吗?brilliant thank you! Just to clarify, all I need to do for flushing rules is to go to the Settings->Permalinks page and hit "Save Changes", correct?
- 0
- 2012-05-25
- Jake
-
您甚至不需要保存更改.只需打开"永久链接"设置页面就足够了(也就是说,如果您的.htaccess文件是可写的.否则,请按保存更改并手动复制它在.htaccess中返回的代码)You don't even need to save changes. It's enough just to open the Permalinks settings page (that is, if your .htaccess file is writable. If not, press save changes and manually copy the code it returns in your .htaccess)
- 4
- 2012-05-25
- 0x61696f
-
这似乎对我不起作用.我的项目帖子仍会转到"example.com/projects/title-of-post".我也访问了永久链接页面.是什么原因造成的?我的htaccess中没有任何重写规则.This doesn't seem to work for me. My projects posts are still going to `example.com/projects/title-of-post`. I visited the Permalinks page too. What could be causing this? There aren't any rewrite rules in my `htaccess`.
- 2
- 2015-01-25
- Desi
-
哇,谢谢,那是缺少的部分!访问永久链接页面无效,只是保存当前的永久链接设置有效:)Wow, thanks that was the missing part! Visiting the permalinks page did not work, but just SAVING the current permalink settings worked :)
- 1
- 2019-02-28
- Alexander Taubenkorb
-
我一直在不改变刷新规则的情况下进行更改.谢谢你的提示!I kept on changing things without flushing the rewrite rules. Thanks for the tip!
- 1
- 2019-11-14
- Tan-007
-
- 2012-05-25
3天前我确实遇到了这个问题,然后偶然发现了一系列 wp.tutsplus.com .我换出了自己的代码以更好地解决您的问题,但这是在完成本系列文章之后的结果.另外,请记住,这未经测试.
// sets custom post type function my_custom_post_type() { register_post_type('Projects', array( 'label' => 'Projects','description' => '', 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'capability_type' => 'post', 'hierarchical' => false, 'publicly_queryable' => true, 'rewrite' => false, 'query_var' => true, 'has_archive' => true, 'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes'), 'taxonomies' => array('category','post_tag'), // there are a lot more available arguments, but the above is plenty for now )); } add_action('init', 'my_custom_post_type'); // rewrites custom post type name global $wp_rewrite; $projects_structure = '/projects/%year%/%monthnum%/%day%/%projects%/'; $wp_rewrite->add_rewrite_tag("%projects%", '([^/]+)', "project="); $wp_rewrite->add_permastruct('projects', $projects_structure, false);
理论上,您可以交换存储在
$projects_structure
变量中的URL中所需的任何内容,而这正是我最终使用的内容.祝您好运,而且一如既往-请务必回来,并告诉我们如何解决! :)
I had this problem literally 3 days ago, then I stumbled across a series over at wp.tutsplus.com. I swapped my own code out to accommodate your question better, but this is what I ended up with after following the series. Also, keep in mind that this is untested.
// sets custom post type function my_custom_post_type() { register_post_type('Projects', array( 'label' => 'Projects','description' => '', 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'capability_type' => 'post', 'hierarchical' => false, 'publicly_queryable' => true, 'rewrite' => false, 'query_var' => true, 'has_archive' => true, 'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes'), 'taxonomies' => array('category','post_tag'), // there are a lot more available arguments, but the above is plenty for now )); } add_action('init', 'my_custom_post_type'); // rewrites custom post type name global $wp_rewrite; $projects_structure = '/projects/%year%/%monthnum%/%day%/%projects%/'; $wp_rewrite->add_rewrite_tag("%projects%", '([^/]+)', "project="); $wp_rewrite->add_permastruct('projects', $projects_structure, false);
Theoretically, you could swap out whatever you want in the URL stored in the
$projects_structure
variable, what is there is just what I ended up using.Good luck, and as always - make sure to come back and let us know how it worked out! :)
-
通常认为仅由链接组成的答案无济于事,因为这些资源将来可能(并且可能会)不再存在.总结内容.Answers that are just composed of links are generally considered unhelpful as those resources can (and probably will) cease to exist in the future. Summarize the content.
- 1
- 2012-05-25
- chrisguitarguy
-
公平地说,我将进行适当的修订.Fair enough, I'll work on a proper revision.
- 0
- 2012-05-25
- cmegown
-
在那里,我的答案现在包含与在生产环境中成功重写自定义帖子类型URL的工作代码相似的代码.希望它能对您有所帮助!There, now my answer contains similar code to working code that I have in a production environment that successfully rewrites a custom post type URL. Hope it proves to be more helpful!
- 11
- 2012-05-25
- cmegown
我为我的投资组合项目设置了一个自定义帖子类型.主要网址位于
/projects/
现在,我还为永久链接结构将博客文章永久链接设置为
/articles/*/
.这意味着当我去查看项目组合项目时,URL更改为/articles/projects/project-name/
我知道对于我的项目自定义帖子类型,必须有一种方法只能仅重写永久链接.但是我不熟悉声明URL slug的语法-希望能得到我的帮助!