我有一个自定义的帖子类型(收藏夹),我计划用它来谈论我最喜欢的工艺品。我想要的meta-box字段是指向在线商店列表的链接,因此用户可以自己购买产品,如果他们选择的话。
下面是自定义帖子类型和所需字段(本质上是一个文本框)的定义。
<?php
function declare_custom_post_type() {
$theme_name = \'thatannalam\';
$custom_post_type = \'favourites\';
$labels = array(
\'name\' => __(\'Favourites\', $theme_name),
\'singular_name\' => __(\'Favourite\', $theme_name),
\'add_new\' => __( \'Add New Favourite\' ),
\'add_new_item\' => __( \'Add New Favourite\' ),
\'edit_item\' => __( \'Edit Favourite\' ),
\'new_item\' => __( \'Add New Favourite\' ),
\'view_item\' => __( \'View Favourite\' ),
\'search_items\' => __( \'Search EvFavouriteent\' ),
\'not_found\' => __( \'No favourites found\' ),
\'not_found_in_trash\' => __( \'No favourites found in trash\' )
);
$args = array(
\'description\' => __( \'Movie news and reviews\', $theme_name),
\'exclude_from_search\' => false,
\'labels\' => $labels,
\'has_archive\' => true,
\'hierarchical\' => false,
\'public\' => true,
\'register_meta_box_cb\' => \'register_meta_box\',
\'rewrite\' => array(\'slug\', $custom_post_type),
\'show_in_admin_bar\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_rest\' => true,
\'show_ui\' => true,
\'supports\' => array(\'title\', \'editor\', \'custom-fields\')
);
register_post_type($custom_post_type, $args);
}
add_action(\'init\', \'declare_custom_post_type\');
function register_meta_box() {
add_meta_box(
\'favourite_details_box\',
\'Favourite Details\',
\'render_meta_html\',
\'favourites\',
\'side\'
);
}
function render_meta_html() {
global $post;
$nonce_key = \'favourite_fields_nonce\';
$link_meta_key = \'product_link_key\';
wp_nonce_field($nonce_key, $nonce_key);
$link = get_post_meta( $post->ID, $link_meta_key, true );
echo \'<label for="product_link_field" class="widefat">Product Link</label><input type="text" id="product_link_field" name="product_link_field" class="widefat" value="\'.$link.\'">\';
}
function save_favourite_meta($post_id, $post) {
$nonce_key = \'favourite_fields_nonce\';
$link_meta_key = \'product_link_key\';
$link_field_name = \'product_link_field\';
$custom_post_type = \'favourites\';
// verify nonce
if (!isset($_POST[$nonce_key]) || !wp_verify_nonce($_POST[$nonce_key], $nonce_key))
return \'nonce not verified\';
// check autosave
if (wp_is_post_autosave($post_id ))
return \'autosave\';
//check post revision
if (wp_is_post_revision($post_id))
return \'revision\';
// check permissions
if ( ! current_user_can( \'edit_post\', $post_id ) ) {
return \'cannot edit post\';
}
$product_link = $_POST[$link_field_name];
if (metadata_exists($custom_post_type, $post_id, $link_meta_key)) {
update_post_meta($post_id, $link_meta_key, $product_link);
} else {
add_post_meta($post_id, $link_meta_key, $product_link);
}
}
add_action( \'save_post\', \'save_favourite_meta\');
?>
困扰我头脑的两个部分是:
检索保存的值。它当前返回一个空白将值保存到DB。我一定错过了什么,因为我得到了一个;更新失败"E;每次尝试保存记录时出错
最合适的回答,由SO网友:Ravi 整理而成
我更改了你的代码,现在它在我的电脑中运行良好。下面是代码
<?php
function declare_custom_post_type() {
$theme_name = \'thatannalam\';
$custom_post_type = \'favourites\';
$labels = array(
\'name\' => __(\'Favourites\', $theme_name),
\'singular_name\' => __(\'Favourite\', $theme_name),
\'add_new\' => __( \'Add New Favourite\' ),
\'add_new_item\' => __( \'Add New Favourite\' ),
\'edit_item\' => __( \'Edit Favourite\' ),
\'new_item\' => __( \'Add New Favourite\' ),
\'view_item\' => __( \'View Favourite\' ),
\'search_items\' => __( \'Search EvFavouriteent\' ),
\'not_found\' => __( \'No favourites found\' ),
\'not_found_in_trash\' => __( \'No favourites found in trash\' )
);
$args = array(
\'description\' => __( \'Movie news and reviews\', $theme_name),
\'exclude_from_search\' => false,
\'labels\' => $labels,
\'has_archive\' => true,
\'hierarchical\' => false,
\'public\' => true,
\'register_meta_box_cb\' => \'register_meta_box\',
\'rewrite\' => array(\'slug\', $custom_post_type),
\'show_in_admin_bar\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_rest\' => true,
\'show_ui\' => true,
\'supports\' => array(\'title\', \'editor\', \'custom-fields\')
);
register_post_type($custom_post_type, $args);
}
add_action(\'init\', \'declare_custom_post_type\');
function register_meta_box() {
add_meta_box(
\'favourite_details_box\',
\'Favourite Details\',
\'render_meta_html\',
\'favourites\',
\'side\'
);
}
function render_meta_html() {
global $post;
$nonce_key = \'favourite_fields_nonce\';
$link_meta_key = \'product_link_key\';
wp_nonce_field($nonce_key, $nonce_key);
$link = get_post_meta( $post->ID, $link_meta_key, true );
echo \'<label for="product_link_field" class="widefat">Product Link</label><input type="text" id="product_link_field" name="product_link_field" class="widefat" value="\'.$link.\'">\';
}
function save_favourite_meta($post_id, $post) {
$nonce_key = \'favourite_fields_nonce\';
$link_meta_key = \'product_link_key\';
$link_field_name = \'product_link_field\';
$custom_post_type = \'favourites\';
// verify nonce
if (!isset($_POST[$nonce_key]) || !wp_verify_nonce($_POST[$nonce_key], $nonce_key))
return \'nonce not verified\';
// check autosave
if (wp_is_post_autosave($post_id ))
return \'autosave\';
//check post revision
if (wp_is_post_revision($post_id))
return \'revision\';
// check permissions
if ( ! current_user_can( \'edit_post\', $post_id ) ) {
return \'cannot edit post\';
}
$product_link = $_POST[$link_field_name];
update_post_meta($post_id, $link_meta_key, $product_link);
}
add_action( \'save_post\', \'save_favourite_meta\', 10,2);
?>
我改变了两件事:
我们必须传递一些参数才能添加\\u action()函数仅使用update\\u post\\u meta()函数如果要同时使用update\\u post\\u meta()和add\\u post\\u meta(),请小心。因为在您的代码中,它在数据库中多次保存值。