在制作wordpress模板時,有時會用到同一個文章需要分開錄入內(nèi)容,分別調(diào)用的情況,這個時候就需要給文章,再添加一個錄入額外內(nèi)容的編輯器。將下面的代碼添加到functions.php中,就可以實現(xiàn)。
function wodepress_post_editor_meta_box() {
add_meta_box (
'wpkj-post-editor',
__('文章頂部內(nèi)容', 'textdomain') ,
'wodepress_post_editor',
'post' // 需要顯示編輯框的文章類型,與下文的兩處 $_POST['post'] 對應(yīng)
);
}
add_action('admin_init', 'wodepress_post_editor_meta_box');
//Displaying the meta box
function wodepress_post_editor($post) {
$content = get_post_meta($post->ID, 'wodepress_post_editor', true);
//This function adds the WYSIWYG Editor
wp_editor (
$content ,
'wodepress_post_editor',
array ( "media_buttons" => true )
);
}
//This function saves the data you put in the meta box
function wodepress_post_editor_save_postdata($post_id) {
if( isset( $_POST['wodepress_post_editor_nonce'] ) && isset( $_POST['post'] ) ) {
//Not save if the user hasn't submitted changes
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Verifying whether input is coming from the proper form
if ( ! wp_verify_nonce ( $_POST['wodepress_post_editor_nonce'] ) ) {
return;
}
// Making sure the user has permission
if( 'post' == $_POST['post'] ) {
if( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
}
$content = get_post_meta($post_id, 'wodepress_post_editor', true);
// 如果編輯器中有內(nèi)容或者之前有數(shù)據(jù)才保存
if( $content || !empty( $_POST['wodepress_post_editor'] ) ) {
$data = $_POST['wodepress_post_editor'];
update_post_meta($post_id, 'wodepress_post_editor', $data);
}
}
add_action('save_post', 'wodepress_post_editor_save_postdata');
添加完了后,在錄入文章時,就可以顯示出來。在此編輯器中錄入內(nèi)容,在需要的地方調(diào)用出來就可以。文章來源:http://www.zghlxwxcb.cn/news/detail-810091.html
<?php
global $post;
$content = get_post_meta( $post->ID, 'wodepress_post_editor', true ); // 獲取字段內(nèi)容
if( $content ) { // 如果有內(nèi)容
echo $content; // 輸出內(nèi)容
}
?>
?原文鏈接?https://www.zhanyes.com/code/6048.html文章來源地址http://www.zghlxwxcb.cn/news/detail-810091.html
到了這里,關(guān)于給wordpress額外添加一個編輯器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!