問題描述
我為我的投資組合項目設置了一個自定義帖子類型其主要 URL 位於/projects /
現在我還設置了我的博客文章永久鏈接到/articles /* /的永久鏈接結構。這意味着當我去查看投資組合項目時,URL 更改為/articles /projects /project-name /
我知道只有我的項目自定義帖子類型,才能重寫永久鏈接。但是我不熟悉聲明 URL 的語法 – 感謝任何幫助我可以得到!
最佳解決方案
當您註冊自定義帖子類型時,必須指定重寫規則不應該添加到現有的 URL 結構中。
簡而言之,這意味着您在 register_post_type 中的這一行調用:
'rewrite' => array('slug' => 'projects'),
應該變成這樣:
'rewrite' => array('slug' => 'projects','with_front' => false),
有關更多信息,請查看 register_post_type 上的 codex 條目中的 rewrite 參數
編輯:只需確保更新代碼後,通過訪問設置> 刷新重寫規則。固定鏈接。否則你仍然會看到舊的鏈接。
次佳解決方案
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 中交換任何您想要的內容,我最終使用的是什麼。
祝你好運,一如既往,一定要回來,讓我們知道如何運作! 🙂
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。