【发布时间】:2017-09-20 16:20:18
【问题描述】:
一直在努力寻找解决当前问题的方法。简单地说,我创建了一个'page-home.php'模板页面,可以获取一个元框来调用页面模板,但是尝试使用元框中的数据更新页面会使数据消失。
代码如下:
function page_add_meta_boxes( $post ) {
global $post;
if(!empty($post))
// get the page template post meta
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
// if the current page uses our specific template, then output our custom metabox
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
// looks for page-home.php file to add our meta box
if($pageTemplate == 'page-home.php' )
{
add_meta_box(
'page-custom-metabox', // $id
'Special Post Meta', // $title
'page_template_metabox', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
}
}
add_action( 'add_meta_boxes_page', 'page_add_meta_boxes' );
function page_template_metabox() {
wp_nonce_field( basename( __FILE__ ), 'page_meta_box_nonce' );
$some_string = get_post_meta( $post->ID, '_some_string', true );
?>
<input type="text" name="some-string" value="<?php echo $some_string; ?>" />
<?php
}
function page_save_custom_post_meta() {
if ( !isset( $_POST['page_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['page_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// check the user's permissions.
if ( ! current_user_can( 'edit_page', $post_id ) ){
return;
}
// save our string
if ( isset( $_REQUEST['some-string'] ) ) {
update_post_meta( $post_id, '_some_string', sanitize_text_field( $_POST['some-string'] ) );
}
}
add_action( 'publish_page', 'page_save_custom_post_meta' );
add_action( 'draft_page', 'page_save_custom_post_meta' );
add_action( 'future_page', 'page_save_custom_post_meta' );
感谢任何建议。
【问题讨论】:
-
试试这个 add_action('save_post ','page_save_custom_post_meta');
-
谢谢你,阿克谢!不幸的是,即使添加了“保存帖子”操作,它仍然不会保存。我不太确定是否有“save_page”操作,但我不相信有。
-
您能否将输入类型名称 some-string 更改为 somestring,请在请求区域进行相应更改。我不确定这个建议
标签: php wordpress metadata custom-post-type