【发布时间】:2016-04-27 17:29:14
【问题描述】:
我创建了一个自定义帖子类型并创建了一个函数,以便它显示在主页上。 这是我的代码:
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;
}
在文档中,我发现我可以使用
访问这些帖子single-{post-type}.php
但如果我创建一个名为
的文件single-recipes.php
我仍然收到“找不到页面”错误。 有没有办法解决这个问题还是我做错了什么?
【问题讨论】:
标签: php wordpress custom-post-type