我添加了一个自定义元框,用于将滑块短代码粘贴到我的页面管理员...我正在尝试将自定义元框保存几个小时。但不知何故它不起作用。我可以发誓我的代码是正确的,但可能不是......任何人都可以帮助我吗?那将是一个很大的好处!谢谢!
<?php
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-masterslider', 'Masterslider Shortcode', 'cd_meta_box_masterslider', 'page', 'normal', 'high' );
}
function cd_meta_box_masterslider()
{
// $post is already set, and contains an object: the WordPress post
global $post;
$values = get_post_custom( $post->ID );
$text = isset( $values['my_meta_box_masterslider'] ) ? $values['my_meta_box_masterslider'] [0] ) :'';
// We'll use this nonce field later on when saving.
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
Falls ein individueller Slider auf der Seite platziert werden soll, bitte hier den Shortcode platzieren.<br />
</p>
<p>
<label for="my_meta_box_masterslider">Masterslider Shortcode</label>
<input name="my_meta_box_masterslider" type="text" value="<?php echo get_post_meta($object->ID, "my_meta_box_masterslider", true); ?>">
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post', $post_id ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchors can only have href attribute
)
);
if( isset( $_POST['my_meta_box_masterslider'] ) )
update_post_meta( $post_id, 'my_meta_box_masterslider', esc_attr( $_POST['my_meta_box_masterslider'], $allowed ) );
}
?>
答案 0 :(得分:0)
cd_meta_box_masterslider函数有一些问题:
您可以使用的完整更新代码...
function cd_meta_box_masterslider()
{
// $post is already set, and contains an object: the WordPress post
global $post;
$values = get_post_custom( $post->ID );
$text = isset($values['my_meta_box_masterslider']) ? $values['my_meta_box_masterslider'][0] : null;
// We'll use this nonce field later on when saving.
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
Falls ein individueller Slider auf der Seite platziert werden soll, bitte hier den Shortcode platzieren.<br />
</p>
<p>
<label for="my_meta_box_masterslider">Masterslider Shortcode</label>
<input name="my_meta_box_masterslider" type="text" value="<?php echo $text; ?>">
</p>
<?php
}