WordPress的 - 访问自定义帖子类型的帖子

问题描述:

我做了一个自定义的帖子类型,并做了一个功能,所以它显示在主页面上。 这是我的代码: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”错误。 有没有办法解决这个问题,或者我做错了什么?

有时您必须进入永久链接设置并将其从默认更改为自定义,然后保存,然后返回并将其更改为默认值。适用于我,当我有这个问题

+0

它实际上工作,谢谢! – BramH

+0

哈哈,我知道它很奇怪 –