Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 具有自己列表的WordPress自定义帖子类型_Php_Wordpress_Plugins_Custom Post Type - Fatal编程技术网

Php 具有自己列表的WordPress自定义帖子类型

Php 具有自己列表的WordPress自定义帖子类型,php,wordpress,plugins,custom-post-type,Php,Wordpress,Plugins,Custom Post Type,我在我的主题中有一个自定义的帖子类型,叫做“recipes”。我可以添加文本、缩略图和添加类别。我想在此帖子类型中添加一个成分列表。我需要一个全球性的成分清单,我可以用一个单一的食谱。如何为自定义帖子类型(如类别)创建全局列表,但仅为成分创建全局列表 以下是我的帖子类型代码: <?php add_action( 'init', 'my_recipes' ); add_filter( 'post_updated_messages', 'my_recipes_messages' ); add_

我在我的主题中有一个自定义的帖子类型,叫做“recipes”。我可以添加文本、缩略图和添加类别。我想在此帖子类型中添加一个成分列表。我需要一个全球性的成分清单,我可以用一个单一的食谱。如何为自定义帖子类型(如类别)创建全局列表,但仅为成分创建全局列表

以下是我的帖子类型代码:

<?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 );

}

如果您想拥有一个可以添加的成分的全局列表,如您所提到的,如类别或标签,那么您需要一个自定义分类法

试试这个工具:

有几个很棒的免费插件,可以让您对字段进行更精确的控制。我建议您研究其中的一种,比如一个字段的选择列表,或者成分的完整分类法或CPT。谢谢!那太有用了!是否有任何选择,我可以添加到配方的成分数量?例如:牛奶=100毫升牛奶?如@chris haas所述,你最好使用ACF。您可能希望创建一个包含两个子字段的Repeater字段—一个文本字段用于数量,另一个文本字段用于选择字段中的所有成分。