0
我將元框添加到我的帖子頁面,以便我可以選擇我對每個帖子的佈局。我創建了一個元框,並顯示,但不會保存。WordPress的帖子佈局元框不保存
if (! function_exists('post_layout_meta_box')){
function post_layout_meta_box($post){
$custom = get_post_custom($post->ID);
$page_layout = isset($custom["page_layout_left"][0]) ? $custom["page_layout_left"][0] : (isset($custom["page_layout_right"][0]) ? $custom["page_layout_right"][0] : $custom["page_layout_none"][0]);
?>
<p>
<select name="page_layout" id="page_layout">
<option value="left_sidebar" <?php selected($page_layout, 'left_sidebar'); ?>>Left Sidebar</option>
<option value="right_sidebar" <?php selected($page_layout, 'right_sidebar'); ?>>Right Sidebar</option>
<option value="full_width" <?php selected($page_layout, 'full_width'); ?>>No Sidebar</option>
</select>
</p>
<?php
}
}
if (! function_exists('post_save_layout_meta_box')){
function post_save_layout_meta_box($post_id){
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return $post_id;
} else{
if(isset($_POST['page_layout'])) {
update_post_meta($post_id, 'page_layout', wp_kses($_POST['page_layout'], ''));
}
}
}
}
add_action('save_post', 'post_save_layout_meta_box');
那麼我錯過了什麼?例如,當我選擇右邊欄時,它會在更新頁面後將值返回到左邊欄。
編輯:想通了:
的問題是這樣的:
$page_layout = isset($custom["page_layout_left"][0]) ? $custom["page_layout_left"][0] : (isset($custom["page_layout_right"][0]) ? $custom["page_layout_right"][0] : $custom["page_layout_none"][0]);
應該
$page_layout = isset($custom["page_layout"][0]) ? $custom["page_layout"][0] : 'left_sidebar';
是的,你是對的,我的if條款是錯的,我想通了。感謝您指出。 – 2014-09-04 09:14:32