0
我添加了一個自定義元框將滑塊簡碼粘貼到我的頁面管理中...我試圖讓我的自定義元框保存幾個小時。但不知何故,它不起作用。我可以發誓我的代碼是正確的,但可能不是......任何人都可以幫助我嗎?這將是一個很大的忙!謝謝!頁面管理中的自定義文本元框不會保存
<?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));
}
?>
$ text = isset($ values ['my_meta_box_masterslider'])? $ values ['my_meta_box_masterslider'] [0]):'';有一個流浪)剛剛在[0]之後,這將打破代碼 –
和<?php echo get_post_meta($ object-> ID,「my_meta_box_masterslider」,true); ?>會拋出一個錯誤,因爲$ object沒有被定義 - 整個塊應該是<?php echo $ text; ?> –
謝謝@SimonPollard。我改變了這一點。不幸的是,我的元框在保存後仍然保持空白。我們可能看不到......這些是我改變的兩行:'$ text = isset($ values ['my_meta_box_masterslider'])? $ values ['my_meta_box_masterslider'] [0]:''; '和'」>' – David