php - 在默认支持中包含的内容之外,如何将特定字段添加到自定义帖子类型?
问题描述
我制作了一个自定义帖子类型并注册了它。我需要针对此帖子类型的更多字段,并希望添加 2 个特定键,这些键将在发布新帖子时加载(IE 就像另一个摘录字段,但它会显示标题/位置)。我不确定如何做到这一点,并且想避免使用插件。任何帮助将不胜感激。
$labels = array(
'name' => _x('Executive', 'post type general name '),
'singular' => _x('Executive', 'post type singular name'),
'add_new' => _x('Add new', 'text_domain'),
'add_new_item' => __( 'Add Executive', 'text_domain' ),
'name_admin_bar' => __( 'Staff', 'text_domain' ),
'archives' => __( 'Staff Archives', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Exectives', 'text_domain' ),
'new_item' => __( 'New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
'menu_name' => __( 'Executives', 'text_domain' ),
);
$args = array(
'label' => __( 'Executive', 'text_domain' ),
'description' => __( 'Executive Team', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'author','name','excerpt','thumbnail', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => false,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'page',
);
register_post_type('Executives', $args);
}
add_action('init', 'custom_post_executive');
解决方案
您需要使用将呈现 HTML 的回调注册自定义元框。您可以在这里阅读更多相关信息:https ://developer.wordpress.org/plugins/metadata/custom-meta-boxes/
我还建议查看插件 Advanced Custom Fields (ACF),因为它们使创建元字段变得微不足道。
function wporg_add_custom_box()
{
$screens = ['post', 'wporg_cpt'];
foreach ($screens as $screen) {
add_meta_box(
'wporg_box_id', // Unique ID
'Custom Meta Box Title', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
$screen // Post type
);
}
}
add_action('add_meta_boxes', 'wporg_add_custom_box');
function wporg_custom_box_html($post)
{
?>
<label for="wporg_field">Description for this field</label>
<select name="wporg_field" id="wporg_field" class="postbox">
<option value="">Select something...</option>
<option value="something">Something</option>
<option value="else">Else</option>
</select>
<?php
}
推荐阅读
- r - 有没有办法在不丢弃数据的情况下处理“无法分配大小向量”问题?
- php - 带有 Foreach 语句的非法字符串偏移 PHP
- javascript - 使用 webpack 或自定义扩展注入一些预格式化的 html
- react-native - React Native 在 4 个特定屏幕之间添加手势
- r - ANOVA 给了我一个显着的 P 值,但 Tukey 的 HSD 将所有内容归类为同一个字母。我做错了什么导致这个?
- php - websockets 连接因 apache 和 Ratchet 而失败
- node.js - 我无法使用 npm 安装Sharp
- firebase - 如何查看邮箱和用户名
- javascript - 如何获得解析服务器查询的结果
- google-visualization - haxis 文本不显示所有记录且不能倾斜