2015-10-06 327 views
2

我有自定義文章類型的WordPress。自定義文章類型 - 檔案

http://localhost/wordpress/photos/ 
http://localhost/wordpress/photos/my-photo 

爲什麼這些url返回404找不到?

如何爲此創建檔案文件? 像:
歸檔photos.php(我有這個目錄,但是...)

function register_gallery(){ 
    register_post_type('photos', array(
     'public'   => true, 
     'menu_position'  => 10, 
     'menu_icon'  => 'dashicons-format-gallery', 
     'supports'  => array('title', 'editor', 'thumbnail'), 
     'has_archive'  => true, 

    )); 
} 

add_action('init','register_gallery'); 
+0

我建議,採取從參考woocommerce,產品是自定義posts.I已完成它的自定義產品類型。 –

+1

我沒有使用任何插件! –

+1

如何在設置>閱讀中設置固定鏈接? – vard

回答

1

你必須使用,

//Need to call when custom post type is being used 
flush_rewrite_rules(); 

也試試,設置 - >永久鏈接保存所有設置。

更多信息請訪問,https://codex.wordpress.org/Function_Reference/register_post_type

+0

訪問永久鏈接頁面是相同的使用'flush_rewrite_rules()' – vard

+0

謝謝我改變固定鏈接 –

+0

@JayDeepNimavat爲什麼呢? 'init'鉤子對於寄存器帖子類型來說非常好。從[WP Codex](https://codex.wordpress.org/Function_Reference/register_post_type):「register_post_type只能通過'init'操作調用」 – vard

0

我發現了另一個解決方案,它可以在404頁通過自定義職位類型(CPT)檔案導航時做到:

/** 
* My cpt is storytitle and slug for cpt is story-title 
* 
**/ 
function generate_cpt_rewrite_rules($wp_rewrite) { 

$event_rules = array(
'story-title/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$' => 'index.php?post_type=storyTitle&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]', 
'story-title/([0-9]{4})/([0-9]{1,2})/?$' => 'index.php?post_type=storyTitle&year=$matches[1]&monthnum=$matches[2]', 
'story-title/([0-9]{4})/?$' => 'index.php?post_type=storyTitle&year=$matches[1]' 
); 

$wp_rewrite->rules = $event_rules + $wp_rewrite->rules; 
} 
add_filter('generate_rewrite_rules', 'generate_cpt_rewrite_rules'); 
相關問題