自定义字段可以扩展文章的信息,也有很多相关的成熟的插件,比如Advanced Custom Fields (ACF) 插件,如果希望添加的字段不多,也不愿意为此安装过多的插件,我们也可以考虑手动来添加它。
为post文章添加字段
// 添加自定义字段输入框
function add_post_custom_fields() {
add_meta_box(
'post_custom_fields', // Meta box ID
'文章自定义字段', // Meta box 标题
'render_post_custom_fields', // 回调函数,用于渲染字段
'post', // 文章类型(这里是 post)
'normal', // 上下文(normal、side、advanced)
'high' // 优先级(high、core、default、low)
);
}
add_action('add_meta_boxes', 'add_post_custom_fields');
// 渲染自定义字段
function render_post_custom_fields($post) {
// 获取已保存的值
$subtitle = get_post_meta($post->ID, 'subtitle', true);
$author_name = get_post_meta($post->ID, 'author_name', true);
// 添加安全字段(防止 CSRF 攻击)
wp_nonce_field('post_custom_fields_nonce', 'post_custom_fields_nonce');
// 输出字段
?>
<label for="subtitle">副标题:</label>
<input type="text" id="subtitle" name="subtitle" value="<?php echo esc_attr($subtitle); ?>" style="width: 100%; margin-bottom: 10px;" />
<label for="author_name">作者姓名:</label>
<input type="text" id="author_name" name="author_name" value="<?php echo esc_attr($author_name); ?>" style="width: 100%;" />
<?php
}
// 保存自定义字段的值
function save_post_custom_fields($post_id) {
// 检查安全字段
if (!isset($_POST['post_custom_fields_nonce']) || !wp_verify_nonce($_POST['post_custom_fields_nonce'], 'post_custom_fields_nonce')) {
return;
}
// 检查用户权限
if (!current_user_can('edit_post', $post_id)) {
return;
}
// 保存副标题
if (array_key_exists('subtitle', $_POST)) {
update_post_meta(
$post_id,
'subtitle',
sanitize_text_field($_POST['subtitle'])
);
}
// 保存作者姓名
if (array_key_exists('author_name', $_POST)) {
update_post_meta(
$post_id,
'author_name',
sanitize_text_field($_POST['author_name'])
);
}
}
add_action('save_post', 'save_post_custom_fields');
自定义文章类型的情况
这里假设我们已经注册了一个名为product
的文章类型:
function add_product_custom_fields() {
add_meta_box(
'product_details', // Meta box ID
'产品详情', // Meta box 标题
'render_product_custom_fields', // 回调函数,用于渲染字段
'product', // 文章类型
'normal', // 上下文(normal、side、advanced)
'high' // 优先级(high、core、default、low)
);
}
add_action('add_meta_boxes', 'add_product_custom_fields');
// 渲染自定义字段
function render_product_custom_fields($post) {
// 获取已保存的值
$price = get_post_meta($post->ID, 'price', true);
$stock = get_post_meta($post->ID, 'stock', true);
// 输出字段
?>
<label for="price">价格:</label>
<input type="number" id="price" name="price" value="<?php echo esc_attr($price); ?>" />
<label for="stock">库存:</label>
<input type="number" id="stock" name="stock" value="<?php echo esc_attr($stock); ?>" />
<?php
}
// 保存自定义字段的值
function save_product_custom_fields($post_id) {
if (array_key_exists('price', $_POST)) {
update_post_meta(
$post_id,
'price',
sanitize_text_field($_POST['price'])
);
}
if (array_key_exists('stock', $_POST)) {
update_post_meta(
$post_id,
'stock',
sanitize_text_field($_POST['stock'])
);
}
}
add_action('save_post', 'save_product_custom_fields');
在模板中使用:
<?php
$price = get_post_meta(get_the_ID(), 'price', true);
$stock = get_post_meta(get_the_ID(), 'stock', true);
if ($price) {
echo '<p>价格: ' . esc_html($price) . '</p>';
}
if ($stock) {
echo '<p>库存: ' . esc_html($stock) . '</p>';
}
?>