2016-01-21 153 views
1

我做了一個自定義的帖子類型,並做了一個功能,所以它顯示在主頁面上。 這是我的代碼:WordPress的 - 訪問自定義帖子類型的帖子

function create_recipe_post_type() { 
    $recipe_labels = array(
     'name'    => 'Recipes', 
     'singular_name'  => 'Recipe', 
     'menu_name'   => 'Recipes', 
     'name_admin_bar' => 'Recipe' 
    ); 

    register_post_type('recipes', 
     $args = array(
      'labels' => $recipe_labels, 
      'public' => true, 
      'supports' => array(
       'title', 
       'editor', 
       'post-formats', 
       'author', 
       'thumbnail', 
       'excerpt', 
       'comments' 
      ), 
      'has_archive' => true, 
      'menu_icon' => 'dashicons-carrot', 
      'query_var' => 'recipes' 
     ) 
    ); 
} 
add_action('init', 'create_recipe_post_type'); 

add_post_type_support('recipe_post_type', 'post-formats'); 

// Add custom post type posts to main page 
add_action('pre_get_posts', 'add_recipe_to_main_page'); 

function add_recipe_to_main_page($query) { 
    if (is_home() && $query->is_main_query()) 
     $query->set('post_type', array('post', 'recipes')); 
    return $query; 
} 

我發現我可以用

單{型後} .PHP

訪問職位文檔內,但如果我創建一個叫做

的文件

single-recipes.php

我仍然收到「Page Not Found」錯誤。 有沒有辦法解決這個問題,或者我做錯了什麼?

回答

1

有時您必須進入永久鏈接設置並將其從默認更改爲自定義,然後保存,然後返回並將其更改爲默認值。適用於我,當我有這個問題

+0

它實際上工作,謝謝! – BramH

+0

哈哈,我知道它很奇怪 –

相關問題