我正在尝试构建我的第一个wordpress插件,目的是创建“食谱”自定义帖子类型。添加测试帖子时遇到了问题。我能够创建新的“食谱”帖子,并将它们显示在“食谱”自定义帖子后端中,但是我无法将自定义帖子添加到常规帖子供稿中,而且帖子链接还会返回“哎呀页面未找到”错误。
简而言之,我能够创建一个新的自定义帖子类型,但是新的自定义帖子不会显示在普通博客摘要中,并且在参与该帖子时创建的自定义帖子会返回找不到页面的错误。
任何见解将不胜感激,谢谢!
<?php
add_action( 'init', 'recipe_custom_post_type');
add_filter( 'post_updated_messages', 'recipes_messages' );
function recipe_custom_post_type() {
$labels = array(
'name' => 'Recipes',
'singular_name' => 'Recipe',
'menu_name' => 'Recipe',
'name_admin_bar'=> 'Recipe',
'add_new' => 'Add New',
'add_new_item' => 'Add New Recipe',
'edit_item' => 'New Recipe',
'view_item' => 'View Recipe',
'all_items' => 'All Recipes',
'search_items' => 'Search Recipes',
'parent_item_colon' => 'Parent Recipes:',
'not_found' => 'No Recipes Found:(',
'not_found_in_trash' => 'No Recipes Found in Trash:('
);
$args = array(
'public' => true,
'labels' => $labels,
'rewrite' => array( 'slug' => 'recipe'),
'has_archive' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-carrot',
'taxonomies' => array( 'post_tag' , 'category'),
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'comments' )
);
register_post_type( 'recipe', $args);
}
function recipes_messages( $messages ) {
$post = get_post();
$messages['recipe'] = array(
0 => '',
1 => 'Recipe Updated.',
2 => 'Custom Field Updated.',
3 => 'Custom Field Deleted.',
4 => 'Recipe Updated.',
5 => isset( $_GET['revision'] ) ? sprintf( 'Recipe restored to revision from %s',wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => 'Recipe Published.',
7 => 'Recipe Saved.',
8 => 'Recipe Submitted.',
9 => sprintf('Recipe scheduled for: <strong>%1$s</strong>.',
date_i18n( 'M j, Y @ G:i', strtotime( $post->post_date ) ) ),
10 => 'Recipe draft updated.'
);
return $messages;
}
?>