2017-04-11 145 views
0

我努力讓我的自定義後類型使用個性化的檔案模板,希望有人能看到我要去哪裏錯了,幫我重回正軌嗎?WordPress的自定義文章類型與類別檔案頁

這裏是我用於創建自定義文章類型代碼:

add_action('init', 'news_post_type'); 
function news_post_type() { 
    register_post_type('news', 
    array(
     'labels' => array(
     'name' => __('News'), 
     'singular_name' => __('News') 
    ), 
     'capability_type' => 'post', 
     'has_archive' => true, 
     'hierarchical' => true, 
     'public' => true, 
     'supports' => array('title','editor','excerpt','trackbacks','custom-fields','revisions','thumbnail','author','page-attributes',) 
    ) 
); 
} 
register_taxonomy('news_category', 'news',array('label' => __('Categories'),'rewrite' => array('slug' => 'news/category'),'hierarchical' => true,)); 

這是偉大的URL回報:www.mysite.com/news/category/%the_category%就像我想。

問題是我希望此CPT中的每個類別都使用我的自定義模板,但是當我創建一個名爲archive-news.php的文件時,它將被忽略。但是,如果我創建一個名爲archive.php的文件,那麼它可以工作,但顯然這適用於我不想要的所有其他文章歸檔。

我不是正確命名模板文件?我是如何創建CPT的錯誤?

如果有人可以幫助將不勝感激。

非常感謝

+0

https://codex.wordpress.org/Category_Templates '類別的slug.php, 品類ID.php, category.php, archive.php, index.php' –

+0

重命名文件類別-news.php沒有工作範疇的任何:( – Ordog

+0

嘗試ID,或在你的archive.php做這樣的事' https://developer.wordpress.org/reference/functions/get_template_part/ –

回答

0

使用在這種情況下工作分類模板,所以命名我的文件taxonomy-news_category.php讓我創建自己的模板,在我的自定義後類型的所有類別。

我還發現,在一個頁面命名一樣的自定義後類型是造成問題了。因此總之,重命名頁面或使用上面的分類模板。

0

我有你的代碼 檢查其在我身邊工作正常。 我想你在詳細頁面上檢查單個$ posttype.php文件。

+0

我不看單頁,絕對是一類:) – Ordog

相關問題