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


Wordpress - Visit custom post type post

我做了一个自定义的帖子类型,并做了一个函数,所以它显示在主页上。这是我的代码:

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

但是如果我创建一个名为

单一食谱.php

我仍然收到"找不到页面"错误。有没有办法解决这个问题,还是我做错了什么?

有时您必须进入永久链接设置并将它们从默认值更改为自定义,然后保存,然后返回并再次将它们更改为默认值。当我遇到此问题时对我有用