2016-04-25 88 views
0

我需要在Wordpress頁面上添加一個字段(文本編輯器),但它只是在頁面上,無論id或slug的具體頁面。如何在單個頁面上添加自定義字段

注意:這是WordPress儀表板中的自定義字段。它應該在我編輯此頁面時顯示。沒有插件,只能通過代碼,因爲我沒有看到需要安裝插件來做到這一點。

我該怎麼做?

預先感謝您!爲所有人而歡呼!

回答

0

嗨到functions.php文件中添加以下代碼

樣子波紋圖像。 enter image description here

/* Define the custom box */ 
add_action('add_meta_boxes', 'myplugin_add_custom_box'); 

/* Do something with the data entered */ 
add_action('save_post', 'myplugin_save_postdata'); 

/* Adds a box to the main column on the Post and Page edit screens */ 
function myplugin_add_custom_box() { 
    add_meta_box('wp_editor_test_1_box', 'WP Editor Test #1 Box', 'wp_editor_meta_box', 'post'); 
} 

/* Prints the box content */ 
function wp_editor_meta_box($post) { 


    // Use nonce for verification 
    wp_nonce_field(plugin_basename(__FILE__), 'myplugin_noncename'); 

    $field_value = get_post_meta($post->ID, '_wp_editor_test_1', false); 

    // Settings that we'll pass to wp_editor 
    $args = array (
     'tinymce' => false, 
     'quicktags' => true, 
); 
    wp_editor($field_value[0], '_wp_editor_test_1', $args); 
} 

/* When the post is saved, saves our custom data */ 
function myplugin_save_postdata($post_id) { 

    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return; 

    // verify this came from the our screen and with proper authorization, 
    // because save_post can be triggered at other times 
    if ((isset ($_POST['myplugin_noncename'])) && (! wp_verify_nonce($_POST['myplugin_noncename'], plugin_basename(__FILE__)))) 
     return; 

    // Check permissions 
    if ((isset ($_POST['post_type'])) && ('page' == $_POST['post_type']) ) { 
    if (! current_user_can('edit_page', $post_id)) { 
     return; 
    }  
    } 
    else { 
    if (! current_user_can('edit_post', $post_id)) { 
     return; 
    } 
    } 

    // OK, we're authenticated: we need to find and save the data 
    if (isset ($_POST['_wp_editor_test_1'])) { 
    update_post_meta($post_id, '_wp_editor_test_1', $_POST['_wp_editor_test_1']); 
    } 

} 
0

我使用WordPress存儲庫中的高級自定義字段(ACF)插件(免費),然後您可以在其中選擇一個字段組,然後選擇所需的所有字段。

對於文本編輯器,您可以使用WSYWIG編輯器作爲字段類型,一旦完成選擇字段,您可以選擇位置作爲您希望顯示的頁面或帖子。

高級自定義字段是根據我的WP前5個插件之一,有這樣的抽出時間和學習它,你可以以各種方式使用它。

希望這有助於

保重,快樂編碼

+0

對不起,我需要沒有插件。只有一個領域的所有人,沒有看到需要嗡嗡插件。 –

+0

抱歉,我從來沒有像其他作者那樣沒有這個插件 –

相關問題