Reputation: 40
I have in my theme a custom post type called "recipes". I can add text, a thumbnail and add categories. I want to add a ingredients list to this post type. I need a global list of ingredients which I can use for a single recipe. How can i create a global list for the custom post type like categories but just for ingredients?
Here's my code for the post type:
<?php
add_action( 'init', 'my_recipes' );
add_filter( 'post_updated_messages', 'my_recipes_messages' );
add_action( 'admin_head', 'my_recipes_help' );
function my_recipes() {
$labels = array(
'name' => 'Recipes',
'singular_name' => 'Recipe',
'menu_name' => 'Recipes',
'name_admin_bar' => 'Recipe',
'add_new' => 'Add New',
'add_new_item' => 'Add New Recipe',
'new_item' => 'New Recipe',
'edit_item' => 'Edit 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(
'labels' => $labels,
'public' => true,
'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')
);
register_post_type( 'my_recipe', $args );
}
function my_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;
}
function my_recipes_help() {
$screen = get_current_screen();
if ( 'recipe' != $screen->post_type ) {
return;
}
$basics = array(
'id' => 'recipe_basics',
'title' => 'Recipe Basics',
'content' => 'Content for help tab here'
);
$formatting = array(
'id' => 'recipe_formatting',
'title' => 'Recipe Formatting',
'content' => 'Content for help tab here'
);
$screen->add_help_tab( $basics );
$screen->add_help_tab( $formatting );
}
Upvotes: 0
Views: 140
Reputation: 881
If you're wanting to have a global list of ingredients that you can add, as you mentioned, like categories or tags, then you're looking for a custom taxonomy.
Try this tool: https://generatewp.com/taxonomy/
Upvotes: 1